在 CoordinatorLayout 中使用 NestedScrollView 和嵌套的 RecyclerView (水平滚动),需要使用 NestedScrolling。

13
我有一个带有CollapsingToolbarLayout的UI设计,如下所示。
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout

        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:theme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="24dp">

        <!-- Hiding unrelated code -->

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/card_margin"
            android:padding="16dp">


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_movie_suggestion"
                    android:layout_width="match_parent"
                    android:layout_height="170dp"
                    android:fillViewport="true"
                    android:nestedScrollingEnabled="false"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

我的问题是,只有当我触摸并滚动RecyclerView之外的区域时,滚动才正常。如果我尝试在RecyclerView内部垂直滚动,滚动就会“被困”,只有NestedScrollView在滚动,CollapsingToolbarLayout没有折叠。


我认为这是一个bug,nestedScrollingEnabled只能在代码中完成。在XML中完成没有任何效果。 - Fung
5个回答

36

您需要以编程方式禁用嵌套滚动。如果在XML中完成,似乎无法正常工作。

recyclerView.setNestedScrollingEnabled(false);

1
我应该将那个无法工作的XML属性视为错误吗? - Fung
如果我的屏幕很大,NestedScrollView可以完全显示(或者大部分显示,如果包括CollaspingToolbarLayout的高度,在底部会有一些过度绘制),那么上述解决方案就不起作用了。似乎没有事件发送到CollaspingToolbarLayout使其折叠。 - Fung
我认为即使没有内部的RecyclerView,这也是正确的。 - tachyonflux
对我有用。非常好的答案。谢谢@karaokyo。 - Saraschandraa
就是这样! 非常感谢 @tachyonflux - rohitpaniker

3

在使用嵌套滚动视图内部的RecyclerView时,这是最常见的问题。

我通过将以下行添加到XML中来解决这个问题。

<android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_comments"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:nestedScrollingEnabled="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

2

嘿,如果你感兴趣,我创建了一个LayoutManager可以解决你的问题。

public class ExpansiveLayoutManager extends LinearLayoutManager {

private int[] mMeasuredDimension = new int[2];

public ExpansiveLayoutManager(Context context) {
    super(context);
}

public ExpansiveLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

public ExpansiveLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@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++) {
        measureScrapChild(recycler, i,
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                mMeasuredDimension);

        if (getOrientation() == HORIZONTAL) {
            width = width + mMeasuredDimension[0];
            if (i == 0) {
                height = mMeasuredDimension[1];
            }
        } else {
            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 != 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);
    }
}
}

https://github.com/emanuelet/LayoutManagers/blob/master/ExpansiveLayoutManager.java


0

您不需要将recyclerView放在NestedScrollView中。

<android.support.v7.widget.RecyclerView
     android:id="@+id/recycler_movie_suggestion"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     android:layout_width="match_parent"
     android:layout_height="170dp" />

0

您不必使用NestedScrollView。只需将LinearLayout保留在那里即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接