RecyclerView与StaggeredGridLayoutManager一起从Glide加载图像

Pas*_*oid 3 android staggered-gridview staggeredgridlayout android-recyclerview android-glide

我有表示问题RecyclerViewStaggeredGridLayoutManager其项包含一个imageview和所述图像中装入imageview使用glide.我面临的问题是,在图像加载后,它们不会交错并且尺寸相同,两列之间也存在很大的差距.如何在列中没有间隙的情况下改变图像的高度?

Don*_*out 8

我遇到了同样的问题.android:adjustViewBounds="true"对我有用的是为我的ImageView 设置.我甚至没有设置scaleType.

这是我的布局文件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"/>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


Pas*_*oid 6

终于,我能够找到灵魂。我必须创建自定义imageview才能在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));
}
}
Run Code Online (Sandbox Code Playgroud)

然后在适配器的onBindViewHolder中,通过-设置图像的长宽比

Picasso.with(this).load(THUMB_URL).into(holder.thumbnailView);
holder.thumbnailView.setAspectRatio(ASPECT_RATIO));
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.. 2年后,您仍然保存我的项目和我的生命:D (2认同)