美文网首页
RecyclerView设置最大高度

RecyclerView设置最大高度

作者: 岩巴上的枯松 | 来源:发表于2020-03-15 21:38 被阅读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);
        }
    }
    

    相关文章

      网友评论

          本文标题:RecyclerView设置最大高度

          本文链接:https://www.haomeiwen.com/subject/kvabehtx.html