簡體   English   中英

另一個父回收者視圖中的回收者視圖

[英]Recycler View inside another Parent Recycler View

我可以在另一個回收站視圖中使用回收站視圖嗎?

我的要求是在屏幕上顯示多個列表:

  • 3個項目的垂直列表

  • 水平項目清單10

  • 5個可擴展項目的垂直列表

  • 再次水平列出5個項目

因此,我當時在想擁有一個MultiRecylerAdapter,它將在內部容納其他Recyler視圖的適配器。

我找到了解決方案。

我們可以將回收器視圖(垂直或水平)插入任何其他回收器視圖中,但是默認的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);
      }
   }
}

2016年3月更新

由Android支持庫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

您可以將A RecyclerView與另一個RecyclerView一起使用。 但是處理這種復雜的情況並不簡單,創建適配器並管理其他布局管理器似乎很復雜。 最好將RecyclerView放在ConstraintLayout / CoOrdinatorLayout中並實現ScrollView。 那要容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM