如何为RecyclerView设置最大高度?

4

我需要实现一个RecylerView,如果它只包含内容高度较小的项,则应该将其包装。如果项增加到像250dp这样的大小,则应将其设置为最大的heght(250dp)并且可以滚动。如何实现?我的父布局是一个Relative layout

这是我的布局文件

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

如果只有一个项目,截图应为wrap_content。

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


请使用ScrollView作为父布局。 - AlphaQ
@AlphaQ ScrollView 中使用 RelativeLayout 或当前 Relative layout 代替。 - Anjal Saneen
4
为什么 Google 要移除 maxHeight,这太不方便了。 - Ian Wambai
3个回答

5

您可以通过编程方式设置 RecylerView 的高度。

如果只有一个项目... 设置为 wrap content

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

否则
<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);

0

我这里有答案:

https://dev59.com/zWMl5IYBdhLWcg3wZWWE#29178364

只需创建一个扩展RecyclerView的新类,并重写它的onMeasure方法即可。
    @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 是以像素为单位的。

你可以使用这个:

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

将250 dp转换为像素


0

1.) 创建一个类来处理将用户传递的最大高度设置为什么:

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.) 将其附加到视图并传递最大高度(以DP为单位):

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接