在RecyclerView中为每一项设置ImageView的大小和Glide,图像大小正在变化

时间:2018-11-14 04:20:11

标签: android android-recyclerview android-glide

在RecyclerView中,我想使用其原始纵横比加载每个项目的图像。服务器不返回图像的大小。

我的代码如下:

@Override
public void onBindViewHolder(K holder, int position) {
    // ....
    Glide.with(mContext)
         .load(imgUrls.get(position))
         .listener(new RequestListener<String, GlideDrawable>(){
            // ...
            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource){
                int drawableWidth = resource.getIntrinsicWidth();
                int drawableHeight = resource.getIntrinsicHeight();
                double ratio = (drawableWidth * 1.0) / (drawableHeight * 1.0);
                // depend on the ratio to set the imageView`s LayoutParams to change the imageView`s size.
                .........
                layoutParams.width = xx;
                layoutParams.height = xx;
                imageView.setLayoutParams(layoutParams);
                return false;
            }
         })
         .into(imageView);
}

但是,当我滚动RecyclerView或刷新RecyclerView时,drawableWidth的值已更改,因此imageView的大小发生了意外的变化。

在RecyclerView中设置ImageView大小的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

尝试这个:

Caused by: org.apache.beam.sdk.Pipeline$PipelineExecutionException: java.lang.RuntimeException:     com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
  "domain" : "global",
​  "message" : "Not found: Table projectId:datasetId.StreamComment",
  "reason" : "notFound"
} ],
  "message" : "Not found: Table projectId:datasetId.StreamComment",
  "status" : "NOT_FOUND"
}

答案 1 :(得分:1)

您可以使用自身滑动设置图像视图的大小。

Glide  
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
.into(imageViewResize);