我在android中使用了recyclelerview和staggredGridLayoutManager。问题是,有时当滚动项目移动以适应屏幕时。通常情况下没什么可担心的,但在我的情况下它会弄乱一切! 那么无论如何都要阻止这种行为?不只是动画。整件物品重新整理东西。 谢谢
答案 0 :(得分:2)
使用Randy回答中建议的ImageView,你可以对Glide做同样的事情:
Glide.with(context)
.load(imageURL)
.asBitmap()
.dontAnimate()
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
if (bitmap != null)
{
holder.image.setHeightRatio(bitmap.getHeight()/bitmap.getWidth());
holder.image.setImageBitmap(bitmap);
}
}
});
答案 1 :(得分:1)
如果您正在使用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() {
}
});
答案 2 :(得分:0)
搜索了很多之后,我意识到没有可行的解决方案! StaggeredGridLayoutManager将重新排列其项目,使该项目四处移动。除非我们编写一个完全自定义的LayoutManager版本不是很容易,否则我怀疑是否有一种方法可以解决这个问题。
答案 3 :(得分:0)
要禁用仅重拨电话
recyclerView.itemAnimator = null
此外,如果您使用ImageView
,请确保将adjustViewBounds
设置为true