RecyclerView是没有maxHeight属性配置的,今天遇到一个需求就需要设置RecyclerView的最大高度,我们可以通过继承RecyclerView自定义实现此属性功能。
一、自定义RecyclerView
public class MaxHeightRecyclerView extends RecyclerView {
private int maxHeight;
public MaxHeightRecyclerView(Context context) {
super(context);
}
public MaxHeightRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightRecyclerView);
maxHeight = arr.getLayoutDimension(R.styleable.MaxHeightRecyclerView_maxHeight, ma