RecyclerView最大高度

先贴两个相关链接。侵删
https://www.jianshu.com/p/cd4f13b01e2c

https://blog.csdn.net/weixiao_812/article/details/89518253

public class MyRecyclerView extends RecyclerView {


    public MyRecyclerView(Context context) {
        super(context);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        if (getChildCount() > 5) {
            View child = getChildAt(0);
            child.measure(widthSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int height = child.getMeasuredHeight() + getPaddingTop() + getPaddingBottom();
            setMeasuredDimension(widthSpec, height * 5);
        } else {
            super.onMeasure(widthSpec, heightSpec);
        }


    }
}

你可能感兴趣的:(RecyclerView最大高度)