maxHeight does not work on RecyclerView

前端 未结 5 1509
情书的邮戳
情书的邮戳 2021-01-02 09:48

I\'ve a DialogFragment.

Layout:




        
相关标签:
5条回答
  • 2021-01-02 10:01

    I just really struggled with this. beware if you are using androidx a few things are different: - first of all use the latest dependency, Im using implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta4" - second mind that in androidx the tags are a bit different

    <androidx.constraintlayout.widget.ConstraintLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_tilemap"
    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="280dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:id="@+id/rv_comms"
            android:padding="8dp"
            android:clipToPadding="false" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    this is what finally worked for me.

    0 讨论(0)
  • 2021-01-02 10:04

    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>
    
    0 讨论(0)
  • 2021-01-02 10:04

    I had the same problem. I extended the RecyclerViewas follows:

    public class MaxHeightRecyclerView extends RecyclerView {
        private static final int MAX_HEIGHT = 765;
    
        public MaxHeightRecyclerView(Context context) {
            super(context);
        }
    
        public MaxHeightRecyclerView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MaxHeightRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthSpec, int heightSpec) {
            heightSpec = MeasureSpec.makeMeasureSpec((int) (MAX_HEIGHT / Resources.getSystem().getDisplayMetrics().density), MeasureSpec.AT_MOST);
            super.onMeasure(widthSpec, heightSpec);
        }
    }
    
    0 讨论(0)
  • 2021-01-02 10:15

    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'
    
    0 讨论(0)
  • 2021-01-02 10:15

    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>
    
    0 讨论(0)
提交回复
热议问题