CoordinatorLayout与NestedScrollView和Horizo ntal Scrolling RecyclerView

问题描述 投票:2回答:2

我有一个带有AppBarLayout的CoordinatorLayout,可以在滚动时折叠/展开工具栏。内容视图是带有RecyclerViews(水平滚动)的NestedScrollView和其他一些没有scrollview的视图。非常类似于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。

android android-layout android-recyclerview android-coordinatorlayout coordinator-layout
2个回答
6
投票

我也有这个问题。

将此属性添加到NestedScrollView标记中

app:layout_behavior="@string/appbar_scrolling_view_behavior"

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

recyclerView.setNestedScrollingEnabled(false);

1
投票

将此属性添加到您的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" />

NestedScrollViewandroid: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">
© www.soinside.com 2019 - 2024. All rights reserved.