ConstraintLayout: Unable to scale image to fit a ratio in RecyclerView : <ul>
<li>I have a recycler view with StaggeredGridLayoutManager. </li>
<li>Within in I have custom items/views. </li>
<li>Each item is defined as a ConstraintLayout where there is an image which is supposed to have a constant aspect ratio.</li>
<li>But the image is to still be able to scale to fit the width of each span. </li>
<li>And then the height should be adjusted according to the ratio. </li>
</ul>

<p>But somehow the images are not maintaining the aspect ratio.</p>

<p>Here are the xml files and code to create the recycler view:</p>

<p>item.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="10dp"&gt;


    &lt;ImageView
        android:id="@+id/image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintDimensionRatio="16:9"
        tools:src="@drawable/ic_logo" /&gt;


    &lt;TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/image"
        android:textColor="@color/white"
        tools:text="SAMPLE"
        android:paddingBottom="10dp"
        android:layout_margin="5dp"/&gt;

&lt;/android.support.constraint.ConstraintLayout&gt;
</code></pre>

<p>activity.xml</p>

<pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"&gt;

    &lt;android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true" /&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p>Code to generate the whole view:</p>

<pre><code>adapter = new SampleAdapter(list);
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        sampleRecyclerView.setLayoutManager(manager);
        sampleRecyclerView.setAdapter(adapter);
</code></pre>

<p>The width of each item is set fine and fits the width of each column span.
But the height is not maintained and is very little. </p>

<p>Could someone please help?</p>

<p>Thanks</p>
