Anjal Saneen
Anjal Saneen

Reputation: 3219

How to set the max height for a RecyclerView?

I have to implementing a RecylerView,it should wrap the content if its contain only item content height is less. If the item's increase like 250dp, it should be set to max heght(250dp) and that be able to scroll.How to achieve this.my parent layout is a Relative layout.

This is My Layout file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom" />

</RelativeLayout>

Screen Shot if its only one item.This should be wrap_content.

Screen Shot if its only one item.This should be wrap_content.

Upvotes: 4

Views: 9907

Answers (3)

babay
babay

Reputation: 4844

I have an answer here:

https://stackoverflow.com/a/29178364/1148784

Just create a new class extending RecyclerView and override it's onMeasure method.

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (maxHeight > 0){
            int hSize = MeasureSpec.getSize(heightMeasureSpec);
            int hMode = MeasureSpec.getMode(heightMeasureSpec);

            switch (hMode){
                case MeasureSpec.AT_MOST:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.AT_MOST);
                    break;
                case MeasureSpec.UNSPECIFIED:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
                    break;
                case MeasureSpec.EXACTLY:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.EXACTLY);
                    break;
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

maxHeight is in pixels.

You can use this:

maxHeight = (int) getContext().getResources().getDisplayMetrics().density * 250;

to convert 250 dp into pixels

Upvotes: 0

Yash
Yash

Reputation: 3576

1.) Create a class to handle setting maximum height to what is passed by the user:

public class OnViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {


private Context context;
private int maxHeight;
private View view;

public OnViewGlobalLayoutListener(View view, int maxHeight, Context context) {
    this.context = context;
    this.view = view;
    this.maxHeight = dpToPx(maxHeight);
}

@Override
public void onGlobalLayout() {
    if (view.getHeight() > maxHeight) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = maxHeight;
        view.setLayoutParams(params);
    }
}

public int pxToDp(int px) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return dp;
}

public int dpToPx(int dp) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return px;
}
}

2.) Attach this to the view and pass the maximum height in DP:

messageBody.getViewTreeObserver()
            .addOnGlobalLayoutListener(
             new OnViewGlobalLayoutListener(messageBody, 256, context)
             );

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

you can programetically set height to RecylerView

if only one item... wrap content

ViewGroup.LayoutParams params=recycler_view.getLayoutParams();
params.height= RecyclerView.LayoutParams.WRAP_CONTENT;
recycler_view.setLayoutParams(params);

else

<dimen name="view_height">250dp</dimen>

float height= getResources().getDimension(R.dimen.view_height); //get height

ViewGroup.LayoutParams params_new=recycler_view.getLayoutParams();
params_new.height=height;
recycler_view.setLayoutParams(params_new);

Upvotes: 5

Related Questions