RecyclerView设置最大高度
2020-03-15 本文已影响0人
岩巴上的枯松
public class MaxHeightRecyclerView extends RecyclerView {
private int mHeight;
public MaxHeightRecyclerView(@NonNull Context context) {
super(context);
}
public MaxHeightRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MaxHeightRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setMaxHeight(int height) {
mHeight = height;
invalidate();
}
private void initAttr(Context context, AttributeSet attrs, int defStyle) {
final TypedArray appearance = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightRecyclerView, defStyle, 0);
mHeight = appearance.getDimensionPixelSize(R.styleable.MaxHeightRecyclerView_rv_max_height, 0);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
if(mHeight > 0) {
heightSpec = MeasureSpec.makeMeasureSpec(mHeight, MeasureSpec.AT_MOST);
}
super.onMeasure(widthSpec, heightSpec);
}
}