嵌套的 RecyclerView 与 CoordinatorLayout

Nested RecyclerView with CoordinatorLayout

我的问题就是这个问题

就像 Google Play 商店一样,我在父 RecyclerView 中有一个嵌套的 RecyclerView(水平)。父 RecyclerView 是 CoordinatorLayout 的子项,当父 RecyclerView 发生滚动时,工具栏会展开和折叠。

在子 RecyclerView 外部触摸并向上滚动一切正常 CollapsingToolbar 折叠但 当我触摸其中一个子 RecyclerView 并向上滚动时 然后父 RecyclerView 滚动并且 CollapsingToolbar 不起作用。

如果需要,我会在这里添加我的源代码..

任何帮助将不胜感激!!

更新:

activity 的 CoordinatorLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coord_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.ChannelHubOld">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        android:fitsSystemWindows="true">
        <RelativeLayout
            android:background="@color/pkDarkGrey"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="200dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/sidebar_header"/>
        </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:theme="@style/ActionBarWidget"/>

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

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

<!-- Parent RecyclerView --->

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

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

勾选Creating Collapsing effect。这可能对你有帮助。

如果 link 损坏,来自 link 的代码。

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>

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

我有一个解决方案给你,我最近尝试过这个,所以它应该有效。

在你的 recyclerview 中,放入你的 nestedScrollView 而不是像 建议的那样。

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                >
            </android.support.v4.widget.NestedScrollView>

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

有同样的问题。 通过在水平嵌套的 RecyclerView 上设置 setNestedScrollingEnabled(false) 修复。似乎 CoordinatorLayout.Behavior 在未设置此项时未正确拦截嵌套滚动。试试吧!


注意:您还必须将布局行为(例如:app:layout_behavior="@string/appbar_scrolling_view_behavior")添加到内部(嵌套)RecyclerView 才能正常工作