Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maxHeight does not work on RecyclerView

I've a DialogFragment.

Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:maxHeight="280dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/line/>

    <View
        android:id="@+id/line"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="@color/black"/>

</android.support.constraint.ConstraintLayout>

I want to initially "wrap recycler view content" (height), but it cannot exceed 280dp. It seems that android:maxHeight has no effect what-so-ever.

like image 486
MaaAn13 Avatar asked Mar 23 '18 12:03

MaaAn13


People also ask

How to set max height of RecyclerView in android?

Set the height of the layout to auto adjust by setting it to 0dp, then set the default layout height constraint as wrap and define a layout max height constraint. Then, on your RecyclerView, set the height to wrap_content and layout_constrainedHeight to true. Hope this helps!

Is RecyclerView deprecated?

Today, suddenly Recyclerview. Viewholder became deprecated. Other, android project is no deprecated.

How to set RecyclerView height programmatically in android?

recyclerview. setLayoutParams(new LayoutParams(LayoutParams. MATCH_PARENT,100));

What is LinearLayoutManager in RecyclerView?

RecyclerView provides these built-in layout managers: LinearLayoutManager shows items in a vertical or horizontal scrolling list. GridLayoutManager shows items in a grid. StaggeredGridLayoutManager shows items in a staggered grid.


1 Answers

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'
like image 110
Ben P. Avatar answered Oct 05 '22 08:10

Ben P.