zoukankan      html  css  js  c++  java
  • Android中当item数量超过一定大小RecyclerView高度固定

    Android中当item数量超过一定大小时,将RecyclerView高度固定

    方法1

    直接通过LayoutParams来设定相应高度
    ViewGroup.LayoutParams lp = rv.getLayoutParams();
    if (list.size() > 4) {
        lp.height = DensityUtil.dip2px(mContext,32 * 4);
    } else {
        lp.height = DensityUtil.dip2px(mContext,34 * list.size());
    }
    rv.setLayoutParams(lp);

    该方法只适用于item高度固定,在本例中使用34dp来设置相应的item高度,故而可以通过乘上相应的item数来计算RecyclerView的高度。

    方法2

    重写LayoutManger的onMeasure方法,这种方式可以获取到各个item的不同高度,从而可以设置变动的高度。

    在使用这种方式时,有一点需要注意的是,不要将RecyclerView的android:layout_height属性设置为wrap_content,不然是不会成功的。

    rv.setLayoutManager(new LinearLayoutManager(mContext) {
                    @Override
                    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
                        int count = state.getItemCount();
                        if (count > 0) {
                            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 : measureWidth;
                                    realHeight += measureHeight;            
                                }
                                setMeasuredDimension(realWidth, realHeight); 
                            }                           
                        } else {
                        super.onMeasure(recycler, state, widthSpec, heightSpec);
                   }
               }
            });

    方法中的recycler是一个item的循环使用器,起到对item管理的作用。

  • 相关阅读:
    51nod 1117 聪明的木匠:哈夫曼树
    51nod 1010 只包含因子2 3 5的数
    51nod 2636 卡车加油
    51nod 2989 组合数
    51nod 2652 阶乘0的数量 V2
    51nod 1103 N的倍数
    51nod 2489 小b和灯泡
    51nod 1003 阶乘后面0的数量
    51nod 2122 分解质因数
    javascript中的setter和getter
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/9103620.html
Copyright © 2011-2022 走看看