android - RecyclerView 与 GridLayoutManager 试图解决 wrap_content

标签 android android-recyclerview gridlayoutmanager

我尝试解决以下问题:

  • 使用 RecyclerView
  • 使用 GridLayoutManager
  • 具有固定的单元格宽度
  • recyclerview 仅调整到必要的高度(wrap_content)

我尝试使用下面的代码,问题是它不起作用,而且我还没有找到任何没有提到在方向改变时正确重绘的工作示例。

public class AutofitRecyclerView extends RecyclerView {
    private MyGridLayoutManager manager;
    private int columnWidth = -1;

    public AutofitRecyclerView(Context context) {
        super(context);
        init(context, null);
    }

    public AutofitRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public AutofitRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            int[] attrsArray = {
                    android.R.attr.columnWidth
            };
            TypedArray array = context.obtainStyledAttributes(attrs, attrsArray);
            columnWidth = array.getDimensionPixelSize(0, -1);
            array.recycle();
        }

        manager = new MyGridLayoutManager(getContext(), 1);
        setLayoutManager(manager);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        super.onMeasure(widthSpec, heightSpec);
        if (columnWidth > 0) {

            int mw = getMeasuredWidth();

            int spanCount = Math.max(1, mw / columnWidth);
            manager.setSpanCount(spanCount);
        }

        int numOfFullRows = manager.getItemCount() / manager.getSpanCount();

        if(manager.getItemCount() % manager.getSpanCount() > 0)
        {
            numOfFullRows++;
        }

        if(getChildCount() > 0) {
            int h = 0;
            try {
                h = manager.getChildAt(0).getMeasuredHeight();
            } catch (Exception e) {
                e.printStackTrace();
            }

            if(h != 0) {
                getLayoutParams().height = numOfFullRows * h;
            }
        }
    }
}

最佳答案

根据连接器发布的代码,我提出了以下支持不同行/列大小的解决方案(尽管我承认没有测试过),考虑了项目装饰的大小并正确处理不同的测量模式.

代码如下:

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

public class WrappableGridLayoutManager extends GridLayoutManager {

    public WrappableGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    private int[] measuredSize = new int[2];

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
        final int widthMode = View.MeasureSpec.getMode(widthSpec);
        final int heightMode = View.MeasureSpec.getMode(heightSpec);
        final int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);

        int spanWidth = 0;
        int spanHeight = 0;

        int viewWidth = 0;
        int viewHeight = 0;

        int spanCount = getSpanCount();

        for (int i = 0; i < getItemCount(); i++) {

            measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredSize);

            if (i % spanCount == 0) {
                spanWidth = measuredSize[0];
                spanHeight = measuredSize[1];
            } else {
                if (getOrientation() == VERTICAL) {
                    spanWidth += measuredSize[0];
                    spanHeight = Math.max(spanHeight, measuredSize[1]);
                } else {
                    spanWidth = Math.max(spanWidth, measuredSize[0]);
                    spanHeight += measuredSize[1];
                }
            }

            if (i % spanCount == spanCount - 1 || i == getItemCount() - 1) {
                if (getOrientation() == VERTICAL) {
                    viewWidth = Math.max(viewWidth, spanWidth);
                    viewHeight += spanHeight;
                } else {
                    viewWidth += spanWidth;
                    viewHeight = Math.max(viewHeight, spanHeight);
                }
            }
        }

        int finalWidth;
        int finalHeight;

        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                finalWidth = widthSize;
                break;
            case View.MeasureSpec.AT_MOST:
                finalWidth = Math.min(widthSize, viewWidth);
                break;
            case View.MeasureSpec.UNSPECIFIED:
                finalWidth = viewWidth;
                break;
            default:
                finalWidth = widthSize;
                break;
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                finalHeight = heightSize;
                break;
            case View.MeasureSpec.AT_MOST:
                finalHeight = Math.min(heightSize, viewHeight);
                break;
            case View.MeasureSpec.UNSPECIFIED:
                finalHeight = viewHeight;
                break;
            default:
                finalHeight = heightSize;
                break;
        }

        setMeasuredDimension(finalWidth, finalHeight);
    }


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

        View view = null;
        try {
            view = recycler.getViewForPosition(position);
        } catch (Exception ex) {
          // try - catch is needed since support library version 24
        }


        if (view != null) {

            RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();

            int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), p.width);
            int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), p.height);

            view.measure(childWidthSpec, childHeightSpec);

            measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
            measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;

            Rect decoratorRect = new Rect();
            calculateItemDecorationsForChild(view, decoratorRect);
            measuredDimension[0] += decoratorRect.left;
            measuredDimension[0] += decoratorRect.right;
            measuredDimension[1] += decoratorRect.top;
            measuredDimension[1] += decoratorRect.bottom;

            recycler.recycleView(view);
        }
    }
}

关于android - RecyclerView 与 GridLayoutManager 试图解决 wrap_content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31681394/

相关文章:

android - 动态设置 recyclerview/cardview 背景颜色

java - 使用 RecyclerView GridLayoutManager 通过 ItemDecoration 制作列间距时 Item 宽度不一样

android - Firebase 中的数据库结构

java - 如何仅在按下按钮时才重新加载 recyclerview?

android - 如何在日 View 中显示日历

android - RecyclerView 中的选择模式?

android - 使用带有 GridLayoutManager 的 RecyclerView 的简单 Android 网格示例(如旧的 GridView)

java - 标签在 Vaadin 的 GridLayout 上消失

android - 飞利浦 Hue Android SDK 检查日落

android - 在处理 Android 模式时 svgs 的 loadShape 失败