RecyclerView with StaggeredGridLayoutManager with images loading from Glide
I was having the same issue. What worked for me was to set android:adjustViewBounds="true"
for my ImageView. I'm not even setting scaleType.
Here's the full content of my layout file, item.xml.
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gif_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorFiller"
android:adjustViewBounds="true"/>
Hope this helps!
Finally i am able to find out the soultion. I had to create the custom imageview to vary its aspect ratio in Staggered Recycler View.
public class DynamicHeightNetworkImageView extends ImageView {
private float mAspectRatio = 1.5f;
public DynamicHeightNetworkImageView(Context context) {
super(context);
}
public DynamicHeightNetworkImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DynamicHeightNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setAspectRatio(float aspectRatio) {
mAspectRatio = aspectRatio;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredWidth = getMeasuredWidth();
setMeasuredDimension(measuredWidth, (int) (measuredWidth / mAspectRatio));
}
}
Then in onBindViewHolder of the adapter i set the aspect ratio of the image by -
Picasso.with(this).load(THUMB_URL).into(holder.thumbnailView);
holder.thumbnailView.setAspectRatio(ASPECT_RATIO));