Stop items from moving around when using StaggeredGridLayoutManager
If you are using Picasso then First create a custom 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() {
}
});
Using the ImageView suggested in Randy's answer 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);
}
}
});