简体   繁体   中英

ImageView in Recycler View - Scrolling causes all images to have same width

I have a Recycler View with images, where the width of the image is variable but the height is fixed. However, scrolling causes all of the images to have the same width. My guess is that the view holder width does not get updated correctly and it's just using the previous one [I'm using an async image loader]. However, clearing out the image drawable, setting it to GONE then VISIBLE again doesn't work either. However, if I turn off my screen and turn it back on, it re-draws correctly.

I have the following inside of my view holder item. Note that the height is fixed but the width is wrap_content. If the height is not fixed and is wrap_content, this issue doesn't repro.

Here's what my item_view.xml looks like:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

   <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:scaleType="centerInside"/>

</RelativeLayout>

From ScaleType documentation its said that,

CENTER_INSIDE: Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

RecycleView use same xml to populate all items hence when it find different dimension in raw images as you have use fixed layout_height , and scale_type center_inside , it will automatically fit the who image into item after scaled.

If you want to use different intem size for different image dimension you have to use custom item view.

CENTER_INSIDE: Scale the image uniformly (maintain the image's aspect ratio) so your imageview always have height = 150dp but width will be different for every image according to your image aspect ratio so width will be =150×(original_image_width×original_image_height )dp . Try imageview scaltype fitXY it keep height 150dp and width equal to original image height.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM