2

我有一个带有 AppBarLayout 的 CoordinatorLayout,可以在滚动时折叠/展开工具栏。内容视图是一个带有 RecyclerViews(水平滚动)的 NestedScrollView 和一些没有滚动视图的其他视图。与 Airbnb 应用程序非常相似。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/toolbar_flat" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
            android:id="@+id/newstedScrollView"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/consistentGreyWhite"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:clipToPadding="false"
                    android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/home_slider"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/consistentWhite"
                        android:clipToPadding="false"
                        android:paddingBottom="@dimen/activity_vertical_margin"
                        android:paddingLeft="14dp"
                        android:paddingRight="14dp"
                        android:paddingTop="@dimen/activity_vertical_margin"
                        />

                    <... other elements ...>

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

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

如果我在没有滚动视图的元素上滚动,则工具栏折叠/展开有效。但是,如果我在 RecyclerView 上执行滚动(垂直),则工具栏不会按预期工作。似乎 RecyclerViews 没有将滚动事件传递给 CoordinatorLayout。

4

2 回答 2

8

我也有这个问题。

将此属性添加到您的NestedScrollView标签

app:layout_behavior="@string/appbar_scrolling_view_behavior"

此外,对片段或活动中的每个 recyclerView 对象执行以下操作。如果您正在嵌套 recycler_views',也可以在您的适配器中。

recyclerView.setNestedScrollingEnabled(false);
于 2016-09-08T09:57:44.330 回答
2

将此属性添加到您的Recycler View.

app:layout_behavior="@string/appbar_scrolling_view_behavior"

您也可以将其添加到Recycler View.

mRecyclerView.setNestedScrollingEnabled(false);

像这样。

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

NestedScrollViewwith也一样android:fillViewport="true"

您可以使用android:fillViewport="true"NestedScrollView测量RecyclerView.

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
于 2016-09-08T10:02:07.047 回答