我在 android 中将 recyclerview 与 staggredGridLayoutManager 结合使用。问题是,有时当滚动项目四处移动以适应屏幕时。通常这没什么好担心的,但在我的情况下,它把一切都搞砸了! 那么有没有办法阻止这种行为呢?不仅仅是动画。整个项目重新排列的东西。 谢谢你
最佳答案
如果您使用的是 Picasso,那么首先创建一个自定义 ImageView
public class DynamicHeightImageView extends ImageView {
private double mHeightRatio;
public DynamicHeightImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DynamicHeightImageView(Context context) {
super(context);
}
//Here we will set the aspect ratio
public void setHeightRatio(double ratio) {
if (ratio != mHeightRatio) {
mHeightRatio = ratio;
requestLayout();
}
}
public double getHeightRatio() {
return mHeightRatio;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mHeightRatio > 0.0) {
// set the image views size
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width * mHeightRatio);
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
然后在你的 onBindViewHolder 中
Picasso.with(context).load(modal.image.mediumUrl).into(holder.profileImage, new Callback() {
@Override
public void onSuccess() {
holder.profileImage.setHeightRatio(holder.profileImage.getHeight()/holder.profileImage.getWidth());
}
@Override
public void onError() {
}
});
关于android - 使用 StaggeredGridLayoutManager 时阻止项目四处移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28523516/