另一个父回收者视图中的回收者视图 [英] Recycler View inside another Parent Recycler View
问题描述
是否可以在另一个回收站视图中使用回收站视图?
Is it possible that I can use a recycler view inside another recycler view ?
我的要求是在一个屏幕上显示多个列表:
My requirements are to show multiple Lists in a screen :
-
3个项目的垂直列表
Vertical List of 3 Items
10个项目的横向列表
Horizontal List of 10 Items
5个可扩展项的垂直列表
Vertical List of 5 Expandable Items
再次获得5个项目的水平清单
Again Horizontal List of 5 Items
所以,我当时在想拥有一个MultiRecylerAdapter,它将在其中容纳其他Recyler视图的适配器.
So, I was thinking to have a MultiRecylerAdapter, which would hold adapters for other recyler views inside.
推荐答案
我找到了解决方案.
我们可以在任何其他回收站视图中插入回收站视图(垂直或水平),但是默认的LinearLayoutManager不支持嵌入式回收站视图的wrap_content高度.
We can insert the recycler view ( either vertical or horizontal) inside any other recyclerview but default LinearLayoutManager does not support wrap_content height of embedded recycler view.
要支持wrap_content,我们应该使用CustomLinearLayoutManager.
To support the wrap_content, we should use CustomLinearLayoutManager for the same.
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);
}
}
}
这篇关于另一个父回收者视图中的回收者视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!