RecyclerView最大高度

2020-02-26  本文已影响0人  细雨么么

先贴两个相关链接。侵删
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);
        }


    }
}

上一篇 下一篇

猜你喜欢

热点阅读