I'm trying to implement a simple grid of images.
Each image is the same size, the problem is that if I scale down the images (for example if I set a spanCount so that the gridlayout has to resize images to fit them) the "virtual-cell" that contains the image does not adapt to image size.
Here you can see the differences between spanCount=3 and spanCount=4.
If I use fitXY for the imageView this is the result, so the grid layout manager reduces the cell width but does not fit the cell height.
My grid layout xml:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4"
tools:itemCount="30"
tools:listitem="@layout/image_item" />
And image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
tools:srcCompat="@tools:sample/avatars" />
My targets:
- STEP 1: remove the gap between columns
- STEP 2: resize the images so that they always fit the horizontal space (in this example with spanCount = 3 a small space is visible on the right)
To be more clear, I need to remove the space marked red.



