How to set RecyclerView Max Height
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 displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int a = (displaymetrics.heightPixels * 60) / 100;
recyclerview1.getLayoutParams().height = a;
But now problem is that, if it have no item then also its height is 60%. So I want to set its height 0 when no item in it.
I want to achieve like something.
if(recyclerview's height > maxHeight)
then set recyclerview's height to maxHeight
else dont change the height of recyclerview.
How can i set it? Please help me, I am stuck with it
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.
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>
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>
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.