How to set RecyclerView maximum height

I want to set the maximum height of the RecylerView. I can set the maximum height using the code below. The code value is 60% of the current screen.

DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int a = (displaymetrics.heightPixels * 60) / 100; recyclerview1.getLayoutParams().height = a; 

But now the problem is that if she does not have an element, then its height is 60%. So I want to set its height to 0 when there is no element in it.

I want to achieve something.

  if(recyclerview height > maxHeight) then set recyclerview height to maxHeight else dont change the height of recyclerview. 

How can i install it? Please help me, I'm stuck with her.

+21
source share
9 answers

Here is what you need. Subclass RecyclerView and override onMeasure as follows:

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

Make sure you specify the correct number of pixels as the first argument to makeMeasureSpec() . I personally needed a RecyclerView, which is no more than 240dps.

+42
source

We can optimize the @Fattum method a bit. We can use app:maxHeight to set maxHeight . You 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> 
+16
source

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> 
+14
source

write these statements inside if else and let me know

 ViewGroup.LayoutParams params=recyclerview.getLayoutParams(); params.height=100; recyclerview.setLayoutParams(params); 
+2
source

You can use WRAP_CONTENT in your RecyclerView. It will automatically measure your recyclerview according to its content.

You can also calculate the current height and set the maximum height. Therefore, Recyclerview will use the value of the wrap_content attribute to the maximum 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); } } 
+1
source

If you have a RecyclerView designed to hold items of equal physical size, and you need an easy way to limit its height without the RecyclerView extension, try the following:

 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 for both Linear and Grid RecyclerViews, you just need to play with numbers a bit to suit your taste. Remember to set the height of WRAP_CONTENT after filling in the RecyclerView.

You can also set the height again every time myElementList is resized, but that is not a big problem.

0
source

The easiest way is to use ConstraintLayout and set a height limit in 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 until Lollipop, the re-view will not scroll. As a solution, add the code below to the create action.

 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 a fixed height only on supported devices, and a full basket view is shown on older devices.

Let me know if there is any better way.

0
source

You can simply set a specific height for any value in the xml code and set the visibility to go away . 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
source

Having tried too many complex sentences, I finally realized this by trial and error.

First, wrap the RecyclerView in some kind of layout. In my case, I used a constraint layout, and I even had other elements in this layout above and below RecyclerView. Set the layout height for automatic adjustment by setting it to 0dp, then set the default layout height limit as a carry and define the maximum layout height limit.

Then in your RecyclerView, set the height of wrap_content and layout_constrainedHeight to true.

Here's how it should look:

 <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
source

Source: https://habr.com/ru/post/1015617/


All Articles