maxHeight does not work on RecyclerView
If you want to achieve this using the XML file, here are the steps. This is for androidx component. First you need to implement a beta version in your gradle file
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta6'
After this, use the following code to create a recyclerView with max height of 240dp
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="240dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try this. To those who did not work on the previews answers.
After some trial and error. Try to add app:layout_constraintHeight_min="100dp"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="400dp"
app:layout_constraintHeight_min="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/list_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
You can achieve this using only XML as long as your parent layout is ConstraintLayout
, which yours seems to be. Make these changes to your RecyclerView
tag:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line/>
The key attributes are these three:
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
Normally you only use a 0dp
dimension when you constrain both sides of the view, but really all you have to do is provide enough constraints to define the view size (and constraining both sides is just the easiest way to do that). Once you have the height set to "match constraints", you can combine a default height constraint with a maximum height constraint to get exactly what you're looking for.
Note you must be using a 1.1 version of the constraint layout library for this. Make sure your app's build.gradle file has something like:
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'