How to set RecyclerView Max Height

前端 未结 12 1624
陌清茗
陌清茗 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:14

    I think this will work at least it worked for me

    <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/Id_const_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                   >
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/Id_my_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin_top_space"
                android:scrollbars="vertical"
                app:layout_constrainedHeight="true"
                app:layout_constraintBottom_toBottomOf="@+id/Id_const_layout"
                app:layout_constraintEnd_toEndOf="@+id/Id_const_layout"
                app:layout_constraintHeight_max="150dp"
                app:layout_constraintHeight_min="30dp"
                app:layout_constraintStart_toStartOf="@+id/Id_const_layout"
                app:layout_constraintTop_toTopOf="@+id/Id_const_layout"
                >
            </androidx.recyclerview.widget.RecyclerView>
                </androidx.constraintlayout.widget.ConstraintLayout>
    

    You can change the constraintHeight_max and constraintHeight_min according to your needs.

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

    The simplest way is to use ConstraintLayout and setting height constraint on Recycleview.

    <android.support.constraint.ConstraintLayout
        android:id="@+id/CL_OUTER_RV_Choose_Categories "
        android:layout_width="match_parent"
        android:layout_height="200dp">
    
       <android.support.v7.widget.RecyclerView
            android:id="@+id/RV_Choose_Categories"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_constrainedHeight="true" >
        </android.support.v7.widget.RecyclerView>
    
    </android.support.constraint.ConstraintLayout>
    

    Please note that before Lollipop, the recycleview will not scroll. As a solution, add the below code in the activity's oncreate.

    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            ConstraintLayout CL_OUTER_RV_Choose_Categories = findViewById(R.id.CL_OUTER_RV_Choose_Categories);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) CL_OUTER_RV_Choose_Categories.getLayoutParams();
            lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        }
    

    This will ensure that height is fixed only on supported devices, and the complete recycle view is shown on older devices.

    Do let me know if there is any better way.

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

    Here is something, you need. Subclass the RecyclerView and override onMeasure as following:

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        heightSpec = MeasureSpec.makeMeasureSpec(Utils.dpsToPixels(240), MeasureSpec.AT_MOST);
        super.onMeasure(widthSpec, heightSpec);
    }
    

    Make sure, you give proper number of pixels as the first argument in makeMeasureSpec(). I personally needed RecyclerView, that is 240dps at most.

    0 讨论(0)
  • We can optimize @Fattum 's method a little bit. We can use app:maxHeight to set the maxHeight. One can use dp or px as you like.

    public class MaxHeightRecyclerView extends RecyclerView {
        private int mMaxHeight;
    
        public MaxHeightRecyclerView(Context context) {
            super(context);
        }
    
        public MaxHeightRecyclerView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initialize(context, attrs);
        }
    
        public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initialize(context, attrs);
        }
    
        private void initialize(Context context, AttributeSet attrs) {
            TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
            mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightScrollView_maxHeight, mMaxHeight);
            arr.recycle();
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            if (mMaxHeight > 0) {
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST);
            }
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

    values/attrs.xml

    <declare-styleable name="MaxHeightScrollView">
        <attr name="maxHeight" format="dimension" />
    </declare-styleable>
    
    0 讨论(0)
  • 2020-12-01 16:27

    If you have a RecyclerView designed to hold items of equal physical size and you want a no-brainer way to limit its height without extending RecyclerView, try this:

    int maxRecyclerViewHeightPixels = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        MAX_RECYCLERVIEW_HEIGHT_DP,
        getResources().getDisplayMetrics()
    );
    
    MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(getContext(), myElementList);
    myRecyclerView.setAdapter(myRecyclerViewAdapter);
    
    ViewGroup.LayoutParams params = myRecyclerView.getLayoutParams();
    
    if (myElementList.size() <= MAX_NUMBER_OF_ELEMENTS_TO_DISPLAY_AT_ONE_TIME) {
        myRecyclerView.setLayoutParams(new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.MATCH_PARENT,
             LinearLayout.LayoutParams.WRAP_CONTENT));
        //LinearLayout must be replaced by whatever layout type encloses your RecyclerView
    }
    else {
        params.height = maxRecyclerViewHeightPixels;
        myRecyclerView.setLayoutParams(params);
    }
    

    This works both for Linear and Grid RecyclerViews, you just need to play with the numbers a bit to suit your taste. Don't forget to set the height to WRAP_CONTENT after you populate the RecyclerView.

    You may also have to set the height again every time the size of myElementList changes, but that's not a big issue.

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

    You can just set a specific height to any value in the xml code and set the visibility to gone. Then you set the visibility to Visible in Java, when you want to inflate it. Here is an example;

    .xml

    android:visibility="gone"
    

    .java

    recyclerView.setVisibility(View.VISIBLE);
    
    0 讨论(0)
提交回复
热议问题