【问题标题】:CoordinatorLayout with NestedScrollView and Horizontal Scrolling RecyclerView带有 NestedScrollView 和水平滚动 RecyclerView 的 CoordinatorLayout
【发布时间】:2017-01-16 04:48:01
【问题描述】:

我有一个带有 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。

【问题讨论】:

  • 试试我的答案。

标签: android android-layout android-recyclerview android-coordinatorlayout coordinator-layout


【解决方案1】:

我也有这个问题。

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

app:layout_behavior="@string/appbar_scrolling_view_behavior"

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

recyclerView.setNestedScrollingEnabled(false);

【讨论】:

  • 感谢 setNestedScrollingEnabled 为我做的。
【解决方案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" />

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">

【讨论】:

  • @Botz 如果某个地方对您有所帮助,请感谢答案:)。
猜你喜欢
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 2016-11-02
  • 2016-12-18
  • 2015-08-28
相关资源
最近更新 更多