【问题标题】:Recycler View inside another Parent Recycler View另一个父回收站视图中的回收站视图
【发布时间】:2017-09-10 20:45:04
【问题描述】:

我可以在另一个回收器视图中使用回收器视图吗?

我的要求是在一个屏幕中显示多个列表:

  • 3 项的垂直列表

  • 10项横向列表

  • 5 个可展开项的垂直列表

  • 再次横向列表5项

所以,我想有一个 MultiRecylerAdapter,它可以容纳内部其他回收器视图的适配器。

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    我找到了解决办法。

    我们可以将回收器视图(垂直或水平)插入到任何其他回收器视图中,但默认的 LinearLayoutManager 不支持嵌入回收器视图的 wrap_content 高度。

    为了支持 wrap_content,我们应该使用 CustomLinearLayoutManager。

     public class CustomLinearLayoutManager extends LinearLayoutManager {
    
     public CustomLinearLayoutManager(Context context, int orientation, boolean 
        reverseLayout)    {
        super(context, orientation, reverseLayout);
    }
    
    private int[] mMeasuredDimension = 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 width = 0;
        int height = 0;
        for (int i = 0; i < getItemCount(); i++) {
    
    
            if (getOrientation() == HORIZONTAL) {
    
                measureScrapChild(recycler, i,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        heightSpec,
                        mMeasuredDimension);
    
                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                measureScrapChild(recycler, i,
                        widthSpec,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);
                height = height + mMeasuredDimension[1];
                if (i == 0) {
                    width = mMeasuredDimension[0];
                }
            }
        }
        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }
    
        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }
    
        setMeasuredDimension(width, height);
    }
    
    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {
        View view = recycler.getViewForPosition(position);
        recycler.bindViewToPosition(view, position);
        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;
            recycler.recycleView(view);
          }
       }
    }
    

    【讨论】:

    • 抛出异常,java.lang.IndexOutOfBoundsException:无效的项目位置 0(0)。商品数量:0
    【解决方案2】:

    2016 年 3 月更新

    由 Android Support Library 23.2.1 的一个支持库版本。所以所有的 WRAP_CONTENT 应该都能正常工作。

    请更新 gradle 文件中的库版本。

      compile 'com.android.support:recyclerview-v7:23.2.1'
    

    这允许 RecyclerView 根据其内容的大小来调整自己的大小。这意味着以前不可用的场景,例如将 WRAP_CONTENT 用于 RecyclerView 的维度,现在可以实现。

    您需要致电

          setAutoMeasureEnabled(true)
    

    修复了与更新中各种测量规范方法相关的错误

    查看http://developer.android.com/intl/es/tools/support-library/features.html#v7-recyclerview

    【讨论】:

      【解决方案3】:

      您可以将 A RecyclerView 与另一个 RecyclerView 一起使用。但是处理这种复杂的情况并不简单。创建适配器和管理不同的布局管理器似乎很复杂。 最好将 RecyclerView(s) 放在 ConstraintLayout/CoOrdinatorLayout 中并实现 ScrollView。这要容易得多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-22
        • 1970-01-01
        • 1970-01-01
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 2016-02-02
        • 1970-01-01
        相关资源
        最近更新 更多