更新1
自Android支持库23.2.0起,添加了setAutoMeasureEnabled(true)
用于LayoutManagers的方法。它使RecyclerView可以包装其内容,并且像一个饰物一样工作。
http://android-developers.blogspot.ru/2016/02/android-support-library-232.html
因此,只需添加如下内容:
LayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setAutoMeasureEnabled(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setNestedScrollingEnabled(false);
更新2
由于setAutoMeasureEnabled
不建议使用27.1.0 ,因此您应该使用覆盖的方法提供LayoutManager的自定义实现isAutoMeasureEnabled()
但是在很多情况下使用RecyclerView之后,我强烈建议不要在包装模式下使用它,因为这不是它的预期用途。尝试使用具有多个项目类型的普通单个RecyclerView重构整个布局。或使用我在下面描述的最后一种方法使用LinearLayout
旧答案(不推荐)
你可以RecyclerView
在里面使用NestedScrollView
。首先,您应该实现自己的custom LinearLayoutManager
,它使您RecyclerView
可以包装其内容。例如:
public class WrappingLinearLayoutManager extends LinearLayoutManager
{
public WrappingLinearLayoutManager(Context context) {
super(context);
}
private int[] mMeasuredDimension = new int[2];
@Override
public boolean canScrollVertically() {
return false;
}
@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(0, View.MeasureSpec.UNSPECIFIED),
heightSpec,
mMeasuredDimension);
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
measureScrapChild(recycler, i,
widthSpec,
View.MeasureSpec.makeMeasureSpec(0, 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);
if (view.getVisibility() == View.GONE) {
measuredDimension[0] = 0;
measuredDimension[1] = 0;
return;
}
// For adding Item Decor Insets to view
super.measureChildWithMargins(view, 0, 0);
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(
widthSpec,
getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view),
p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(
heightSpec,
getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(view),
p.height);
view.measure(childWidthSpec, childHeightSpec);
// Get decorated measurements
measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
}
之后,将其LayoutManager
用于您的RecyclerView
recyclerView.setLayoutManager(new WrappingLinearLayoutManager(getContext()));
但是您还应该调用这两个方法:
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);
在此setNestedScrollingEnabled(false)
禁用的滚动RecyclerView
,因此它不会拦截来自的滚动事件NestedScrollView
。并setHasFixedSize(false)
确定适配器内容的更改可以更改RecyclerView
重要说明:此解决方案在某些情况下几乎没有错误,并且在性能方面存在问题,因此,如果您有很多项目,RecyclerView
建议您使用LinearLayout
基于自定义的列表视图实现,为其创建适配器的类似物并使其表现得像ListView
或RecyclerView