Android 从Glide加载图像的带交错EdgridLayoutManager的RecyclerView
我在使用Android 从Glide加载图像的带交错EdgridLayoutManager的RecyclerView,android,android-recyclerview,android-glide,staggered-gridview,staggeredgridlayout,Android,Android Recyclerview,Android Glide,Staggered Gridview,Staggeredgridlayout,我在使用StaggedGridLayoutManager显示RecyclerView时遇到问题,该管理器的项包含imageview,并且使用glide将图像加载到imageview。 我面临的问题是,在加载图像后,它们不是交错的,而且大小相同,两列之间有一个很大的间隙。如何在列中没有间隙的情况下改变图像的高度?首先初始化您的RecyclerView RecyclerView recyclerGallery = (RecyclerView)findViewById(R.id.gallery_re
StaggedGridLayoutManager
显示RecyclerView
时遇到问题,该管理器的项包含imageview
,并且使用glide
将图像加载到imageview
。
我面临的问题是,在加载图像后,它们不是交错的,而且大小相同,两列之间有一个很大的间隙。如何在列中没有间隙的情况下改变图像的高度?首先初始化您的RecyclerView
RecyclerView recyclerGallery = (RecyclerView)findViewById(R.id.gallery_recycler_view);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(3, 1);
recyclerGallery.setLayoutManager(gaggeredGridLayoutManager);
galleryAdapter = new GalleryAdaptor(ProfImageGallery.this, imgsUrl);
recyclerGallery.setAdapter(galleryAdapter);
Picasso.with(mContext)
.load(imageUrl)
.into(holder.workImage,
new ImageLoadedCallback(holder.progressBar) {
@Override
public void onSuccess() {
if (holder.progressBar != null) {
holder.progressBar.setVisibility(View.GONE);
}
}
});
然后选择适配器(根据需要进行编辑)
滑动。
Glide.with(this).load(imageUrl).into(imageView);
我终于找到了答案。我必须创建自定义imageview,以改变交错回收器视图中的纵横比
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));
}
}
然后在适配器的onBindViewHolder中,我将图像的纵横比设置为-
Picasso.with(this).load(THUMB_URL).into(holder.thumbnailView);
holder.thumbnailView.setAspectRatio(ASPECT_RATIO));
我也有同样的问题。对我来说,有效的方法是为我的ImageView设置
android:adjustViewBounds=“true”
。我甚至没有设置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"/>
希望这有帮助 将
android:adjustViewBounds=“true”
添加到项目布局的图像视图中,并在Glide中使用CenterCrop
和FitCenter
,如下所示:
Glide.with(vh.view.getContext())
.load(item.getUrl())
.centerCrop()
.fitCenter()
.thumbnail(0.3f)
.placeholder(R.drawable.img_placeholder)
.into(vh.image);
对我来说,它起作用了
Picasso.get()
.load(uploadCurrent.getImageUrl())
.占位符(R.可绘制.加载)
.config(Bitmap.config.RGB_565)
.插入(支架.图像视图)
如果列之间的间距很大,则还应为图像提供scaleType
,我建议您设置centerCrop或fitXY scaleType。提供您作品的任何图像+代码都会更容易理解。您可以在这里查看一下,您可以使用glide和R.layout.row_gallery的布局是什么?这是在recycler View中使用的布局行谢谢。。2年后,你仍然拯救了我的项目&我的生命:如何通过纵横比?如何得到它?
Glide.with(vh.view.getContext())
.load(item.getUrl())
.centerCrop()
.fitCenter()
.thumbnail(0.3f)
.placeholder(R.drawable.img_placeholder)
.into(vh.image);