How to set RecyclerView Max Height

前端 未结 12 1623
陌清茗
陌清茗 2020-12-01 15:55

I want to set Max Height of RecylerView.I am able to set max height using below code.Below code makes height 60% of current screen.

     DisplayMetrics displ         


        
相关标签:
12条回答
  • 2020-12-01 16:04

    write this statements inside if else and let me know ,

    ViewGroup.LayoutParams params=recyclerview.getLayoutParams();
    params.height=100;
    recyclerview.setLayoutParams(params);
    
    0 讨论(0)
  • 2020-12-01 16:07

    ConstraintLayout offers maximum height for its children.

    <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">
    
        <ListView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            app:layout_constrainedHeight="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_max="300dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    
    0 讨论(0)
  • You can use WRAP_CONTENT in your RecyclerView. It will auto measure your recyclerview according to its content.

    You can also calculate current height and set max height. So Recyclerview will use wrap_content attribute value until max height.

    public static void getTotalHeightofRecyclerView(RecyclerView recyclerView) {
    
            RecyclerView.Adapter mAdapter = recyclerView.getAdapter();
    
            int totalHeight = 0;
    
            for (int i = 0; i < mAdapter.getItemCount(); i++) {
                View mView = recyclerView.findViewHolderForAdapterPosition(i).itemView
    
                mView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    
                totalHeight += mView.getMeasuredHeight();
            }
    
            if (totalHeight > 100) {
                ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
                params.height = 100;
                recyclerView.setLayoutParams(params);
            }
        }
    
    0 讨论(0)
  • 2020-12-01 16:09

    I just had this same problem, and wanted to share a new, up-to-date answer. This one works on at least Android 9 and Android Studio v. 4.0.

    Wrap the RecyclerView in a ConstraintLayout, as some of the answers here have suggested. But it does not appear you can set the maximum height of that layout in XML. Instead, you must set it in code, as follows: Give the ConstraintLayout an ID, and then, in your onCreate or onViewCreated method for the activity or fragment, respectively, in question - for example, to set a max height of 200 dp:

    findViewById<ConstraintLayout>(R.id.[YOUR ID]).maxHeight = 200 // activity
    view.findViewById<ConstraintLayout>(R.id.[YOUR ID]).maxHeight = 200 // fragment
    

    Annoyingly, ConstraintLayout does expose an XML attribute android:minHeight but no android:maxHeight.

    Even more annoyingly, RecyclerView itself apparently comes with an XML attribute named android:maxHeight, but that attribute does not work.

    0 讨论(0)
  • 2020-12-01 16:11

    ill add my 2 cents i needed a recycler view with a max height and min height and wrap content between the 2

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:id="@+id/group_card_recycler_view_holder"
                app:layout_constraintHeight_default="wrap"
                app:layout_constraintHeight_max="@dimen/group_recycler_view_height"
                app:layout_constraintHeight_min="@dimen/card_sentence_height"
                android:layout_marginTop="@dimen/activity_horizontal_margin_8dp"
                app:layout_constraintTop_toBottomOf="@id/group_name_container"
                app:layout_constraintBottom_toTopOf="@id/myButtonLayout">
    
    
                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_constrainedHeight="true"
                    android:id="@+id/group_card_recycler_view"/>
    
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    
    0 讨论(0)
  • 2020-12-01 16:12

    After trying way too many complicated suggestions, I finally figured this out through trial and error.

    First, wrap the RecyclerView in some sort of layout. In my case, I used a constraint layout, and I even had other elements in that layout above and below the RecyclerView. 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.

    Here is what it should look like:

    <android.support.constraint.ConstraintLayout
        android:layout_width="600px"
        android:layout_height="0dp"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="450px">
    
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constrainedHeight="true"
            app:layout_constraintTop_ToTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题