自定义RecycleView能显示的最大高度

系列文章目录

今天项目里有一个需求,就是Dialog里加载一个RecycleView,但是Dialog能显示最大的高度是有限制的。当RecycleView展示的内容没达到最大高度时,RecycleView显示的大小随着Item的个数变化。

提示:以下是本篇文章正文内容,下面案例可供参考

一、pandas是什么?

自定义的RecycleView

二、使用步骤

1.引入库

代码如下(示例):

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.MaxHeightRecyclerView);
        mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightRecyclerView_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);
    }
}

2.在布局文件里使用

代码如下(示例):

    <com.geek.supercashier.widget.MaxHeightRecyclerView
        app:maxHeight="1000dp"
        android:id="@+id/popRelv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/whites" />

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了自定义的最大高度的RecycleView。

本文地址:https://blog.csdn.net/github_38016668/article/details/109627287