RecyclerView Recycled ViewHolder Image View wrong size

后端 未结 5 1070
眼角桃花
眼角桃花 2020-12-31 14:54

I have a recycler view with different View Holders.

A couple of view holders have image views which I pass into Glide to display images.

The problem is, when

相关标签:
5条回答
  • 2020-12-31 15:30

    Add this line

    android:adjustViewBounds="true"
    

    to the imageview in layout file it will automatically resize image view.

    in glide change .crossFade() to .fitCenter()

    0 讨论(0)
  • 2020-12-31 15:32

    1 . add android:adjustViewBounds="true" to ImageView

    <ImageView
        android:id="@+id/img_item_my_show_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/backgrund_banner"/>
    

    2.Change the following code to Glide

    Glide.with(context).asBitmap().load(imgUrl)
            .apply(RequestOptions()
                    .fitCenter()
                    .placeholder(R.drawable.default_img)
                    .error(R.drawable.error_img))
            .into(ImageView)
    
    0 讨论(0)
  • 2020-12-31 15:41

    I ran into the same issue and i solved it as below,

    Glide.with(mContext)
       .load(model.getImage())
       .asBitmap()
       .fitCenter()
       .placeholder(R.drawable.ic_placeholder)
       .error(R.drawable.ic_placeholder)
       .into(holder.ivImage);
    

    I just added .asBitmap() and .fitCenter() , Issue Resolved.

    0 讨论(0)
  • 2020-12-31 15:42

    you must in onBindViewHolder set width of image

    for example:

    yourImageView.getLayoutParams().width = GetScreenWidthPx();
    
    public int GetScreenWidthPx() {
         DisplayMetrics displayMetrics = MyApp.GetContext().getResources().getDisplayMetrics();
         return displayMetrics.widthPixels - DpToPx(your_margin_in_dp);
    }
    
    public static int DpToPx(int dp) {
        DisplayMetrics displayMetrics =
               MyApp.GetContext()
               .getResources()
               .getDisplayMetrics();
        return (int) (dp * displayMetrics.density + 0.5f);
    }
    
    0 讨论(0)
  • 2020-12-31 15:50

    Try giving your recyclerview's item ImageView an attribute as: android:scaleType="fitXY"

    0 讨论(0)
提交回复
热议问题