RecycleView根据内容自适应高度,最大高度限制

前言

recylceView 日常开发经常使用到,默认高度自适应,如何增加最大高度限制如下:

public class MeasureLinearLayoutManager extends LinearLayoutManager {
    private int maxHeight;

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

    public MeasureLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public MeasureLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public int getMaxHeight() {
        return maxHeight;
    }

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }

    @Override
    public void setMeasuredDimension(int widthSize, int heightSize) {
        if (maxHeight == 0) {
            super.setMeasuredDimension(widthSize, heightSize);
        } else {
            super.setMeasuredDimension(
            widthSize, heightSize < maxHeight ? heightSize : maxHeight);
        }
    }
}

你可能感兴趣的:(android,基础知识)