更新1
自Android支持库23.2.0以来,为布局管理器添加了setAutoMeasureEnabled(true)方法。它使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
因为27.1.0的setAutoMeasureEnabled已弃用,所以你应该提供LayoutManager的自定义实现,覆盖方法isAutoMeasureEnabled()
但是经过多次使用RecyclerView后,我强烈建议不要在包装模式下使用它,因为这不是它的目的。尝试使用普通的单个RecyclerView与几个项目类型重构整个布局。或者使用线性布局的方法,我在下面描述作为最后的手段
旧答案(不推荐)
你可以在NestedScrollView中使用RecyclerView。
首先,你应该实现你自己的自定义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的大小
重要提示:这个解决方案是小bug在某些情况下,有问题与性能,所以如果你有很多项目在你的RecyclerView,我建议使用自定义线性布局的实现列表视图,为它创建类似的适配器,使其行为像ListView或RecyclerView