zoukankan      html  css  js  c++  java
  • Android开发 在有指定数量item后固定RecyclerView高度

    前言

      下面提供了2种方式,看情况选择使用。

    代码部分

    方式一

    可靠且简单暴力的方式,需要让RecyclerView先setAdapter();

            mRecyclerview.post(new Runnable() { //将修改高度的代码放到RecyclerView最后面执行,让RecyclerView先测量完毕
                @Override
                public void run() {
                    if (mRecyclerview.getAdapter() == null || mRecyclerview.getAdapter().getItemCount() <= 4){ //小于4个不固定高度
                        return;
                    }
                    View view = mRecyclerview.getChildAt(0);
                    if (view == null){
                        return;
                    }
                    int height = view.getHeight() * 4;
                    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) mRecyclerview.getLayoutParams();
                    layoutParams.height = height;
                    mRecyclerview.setLayoutParams(layoutParams);
                }
            });

     

    方式二

    可能会被父类的布局测量影响

    使用时,请注意2点情况

    1. 不要将RecyclerView的android:layout_height属性设置为wrap_content,不然是不会成功的.
    2. item的根布局也需要固定高度,不要使用wrap_content,否则下面测算高度时会出现不准确的情况.
    复制代码
        /**
         * 自适应列表View在到指定数量item后固定高度,
         *
         * @param targetNum
         */
        private void adaptiveRecyclerViewHeight(int targetNum) {
            mFamilyListRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()) {
                @Override
                public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
                    int count = state.getItemCount();
                    if (count > 0) {
                        if (count > targetNum) {
                            count = targetNum;
                        }
                        int realHeight = 0;
                        int realWidth = 0;
                        for (int i = 0; i < count; i++) {
                            View view = recycler.getViewForPosition(0);
                            if (view != null) {
                                measureChild(view, widthSpec, heightSpec);
                                int measuredWidth = View.MeasureSpec.getSize(widthSpec);
                                int measuredHeight = view.getMeasuredHeight();
                                realWidth = realWidth > measuredWidth ? realWidth : measuredWidth;
                                realHeight = realHeight + measuredHeight;
                            }
                        }
                        setMeasuredDimension(realWidth, realHeight);
                    } else {
                        super.onMeasure(recycler, state, widthSpec, heightSpec);
                    }
                }
            });
        }
    复制代码

    End

  • 相关阅读:
    币值转换
    抓老鼠啊!亏了还是赚了
    打印沙漏
    秋季学习总结
    记忆中最深刻的三位老师
    自我介绍
    docker 安装redis 和 mysql
    centos 安装docker
    celery的简单使用
    django redis配置和简单使用
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/13037156.html
Copyright © 2011-2022 走看看