Stop moving items when using StaggeredGridLayoutManager

I am using recyclerview with staggredGridLayoutManager in android. The problem is that sometimes when the scroll elements move to fit the screen. Usually this is nothing to worry about, but in my case it messed up! Anyway, to stop this behavior? Not just animation. All things rearrange things. thanks

+8
android android-recyclerview staggered-gridview
source share
2 answers

Using the ImageView suggested by Randy, you can do the same with 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); } } }); 
+2
source share

If you are using Picasso, first create your own 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); } } } 

Then in your 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() { } }); 
+1
source share

All Articles