[英]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 : 我的要求是在屏幕上显示多个列表:
Vertical List of 3 Items 3个项目的垂直列表
Horizontal List of 10 Items 水平项目清单10
Vertical List of 5 Expandable Items 5个可扩展项目的垂直列表
Again Horizontal List of 5 Items 再次水平列出5个项目
So, I was thinking to have a MultiRecylerAdapter, which would hold adapters for other recyler views inside. 因此,我当时在想拥有一个MultiRecylerAdapter,它将在内部容纳其他Recyler视图的适配器。
I found the solution. 我找到了解决方案。
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. 我们可以将回收器视图(垂直或水平)插入任何其他回收器视图中,但是默认的LinearLayoutManager不支持嵌入式回收器视图的wrap_content高度。
To support the wrap_content, we should use CustomLinearLayoutManager for the same. 为了支持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);
}
}
}
UPDATE March 2016 2016年3月更新
By Android Support Library 23.2.1 of a support library version. 由Android支持库23.2.1提供的支持库版本。 So all WRAP_CONTENT should work correctly. 因此,所有WRAP_CONTENT均应正常工作。
Please update version of a library in gradle file. 请在gradle文件中更新库的版本。
compile 'com.android.support:recyclerview-v7:23.2.1'
This allows a RecyclerView to size itself based on the size of its contents. 这允许RecyclerView根据其内容的大小自行调整大小。 This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. 这意味着以前不可用的方案,例如现在可以使用WRAP_CONTENT作为RecyclerView的尺寸。
you'll be required to call 您将需要致电
setAutoMeasureEnabled(true)
Fixed bugs related to various measure-spec methods in update 修复了与更新中的各种度量规范方法有关的错误
Check http://developer.android.com/intl/es/tools/support-library/features.html#v7-recyclerview 检查http://developer.android.com/intl/es/tools/support-library/features.html#v7-recyclerview
You can use A RecyclerView withing another RecyclerView . 您可以将A RecyclerView与另一个RecyclerView一起使用。 But handling this complex situation is not simple.Creating adapter(s) and managing different layout manager seems complex. 但是处理这种复杂的情况并不简单,创建适配器并管理其他布局管理器似乎很复杂。 Better to put the RecyclerView(s) in a ConstraintLayout/CoOrdinatorLayout and implement ScrollView. 最好将RecyclerView放在ConstraintLayout / CoOrdinatorLayout中并实现ScrollView。 That's much more easier. 那要容易得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.