Search code examples
androidimageviewandroid-recyclerviewstaggered-gridviewstaggeredgridlayout

Android scale item height of RecyclerView with StaggeredGridLayoutManager to match column width


I am using RecyclerView with StaggeredGridLayoutManager in vertical orientation. Item of the RecyclerView contains only one view - ImageView. I would like the ImageView height to scale to match RecyclerView's column width and keep aspect ratio of photo that it contains. Currently parameters of this image view are: android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerCrop" but the height of the item in RecyclerView matches original height of the photo and the photo is just centered. How is it possible to achieve that?


Solution

  • Add this attribute to your ImageView:

        android:adjustViewBounds="true"
    

    Image View | Android Developers