网格布局的RecyclerView高度随Item自适应
博客专区 > xunfang 的博客 > 博客详情
网格布局的RecyclerView高度随Item自适应
xunfang 发表于2年前
网格布局的RecyclerView高度随Item自适应
  • 发表于 2年前
  • 阅读 2941
  • 收藏 4
  • 点赞 1
  • 评论 5

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 网格布局的RecyclerView高度随Item自适应

表格样式的RecyclerView高度随Item自适应 的解决办法就是自己去重写布局管理器GridLayoutManager。

package com.art.artcircle.widget;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
public class WrapHeightGridLayoutManager extends GridLayoutManager {

    private int mChildPerLines;
    private int[] mMeasuredDimension = new int[2];

    public WrapHeightGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
        this.mChildPerLines = spanCount;
    }

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {

        final int heightMode = View.MeasureSpec.getMode(heightSpec);
        final int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);
        int height = 0;
        for (int i = 0; i < getItemCount(); ) {
            measureScrapChild(recycler, i,
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    mMeasuredDimension);
            height = height + mMeasuredDimension[1];
            i = i + mChildPerLines;
        }

        // If child view is more than screen size, there is no need to make it wrap content. We can use original onMeasure() so we can scroll view.
        if (height >heightSize) {
            switch (heightMode) {
                case View.MeasureSpec.EXACTLY:
                    height = heightSize;
                case View.MeasureSpec.AT_MOST:
                case View.MeasureSpec.UNSPECIFIED:
            }
            setMeasuredDimension(widthSize, height);
        } else {
            super.onMeasure(recycler, state, widthSpec, heightSpec);
        }
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {

        View view = recycler.getViewForPosition(position);

        // For adding Item Decor Insets to view
        super.measureChildWithMargins(view, 0, 0);
        if (view != null) {
            RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
            int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                    getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view), p.width);
            int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                    getPaddingTop() + getPaddingBottom() + getPaddingBottom() + getDecoratedBottom(view), p.height);
            view.measure(childWidthSpec, childHeightSpec);

            // Get decorated measurements
            measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
            measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
            recycler.recycleView(view);
        }
    }
}
在Activity中这样使用:

 WrapHeightGridLayoutManager gm = new WrapHeightGridLayoutManager(mContext, Constant.RECYCLERVIEW_SIZE_GRIDLAYOUT_FIVE);
 mPhotosRcv.setLayoutManager(gm);

这样就完美实现你想要的RecyclerView高度随Item自适应效果了。

共有 人打赏支持
粉丝 0
博文 50
码字总数 7950
评论 (5)
gexingtoday
你确定可以用你自己试过了吗
gexingtoday
我的是在RecyclerView中Item再嵌套网格的RecyclerView,XML宽高都是wrap_content,显示不出来,用你的这个方法重写GridLayoutManager类,一样不显示,能问下怎么解决
gexingtoday
哥们,你 if (height < heightSize)这句是不是错了,改成 if (height > heightSize)就好使了
xunfang

引用来自“gexingtoday”的评论

哥们,你 if (height < heightSize)这句是不是错了,改成 if (height > heightSize)就好使了
哦哦,是吗,可能有bug,谢谢纠正,有问题积极指出,谢谢~\(≧▽≦)/~啦
TNT_GG
你这代码测试过没?有重大BUG,运行直接ANR
×
xunfang
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: