带有CoordinatorLayout的嵌套RecyclerView

27

我的问题就像在这里问的问题一样:嵌套的水平滚动RecyclerView中的滚动行为

类似于Google Play商店UI,我有一个嵌套的水平RecyclerView,位于一个父RecyclerView(垂直滚动)中。父RecyclerView是CoordinatorLayout的子项,在其中工具栏会在滚动父RecyclerView时展开和折叠。

当我们触摸子RecyclerView之外(请参考下面的图1),然后向上滚动时,一切都很正常,CollapsingToolbar会被折叠,但是当我触摸其中一个子RecyclerView并向上滚动时,CollapsingToolbar不会折叠。

[下面的图像显示了当我们在Featured和Trending之间触摸并向上滚动时]enter image description here

[下面的图像显示了当我们触摸特色列表并向上滚动时]enter image description here

更新:

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>
4个回答

55

我遇到了同样的问题。 通过对水平嵌套的RecyclerView设置setNestedScrollingEnabled(false)来解决。如果不这样做,似乎CoordinatorLayout.Behavior无法正确地截取嵌套滚动。试一下吧!


注意:您还需要为内部(嵌套)RecyclerView添加布局行为 (例如:app:layout_behavior="@string/appbar_scrolling_view_behavior") 才能使其正常工作。


2
是的,我已经解决了!我需要在xml中为NestedRecycler应用layoutBehavior,然后将setNestedScrollingEnabled设置为false。 - Rafique Mohammed
@fast3r 你是我的英雄。这必须是一个被接受的答案——完全正确。 - Konstantin Loginov
我必须补充一点,在API 19(和可能是API 17)中,在XML中设置它不起作用。相反,在代码中设置才能真正发挥作用。 - terencey
@squeeish 仅在API 21及以上版本中支持XML。 - android developer
1
即使在内部recyclerview中没有"app:layout_behavior="@string/appbar_scrolling_view_behavior"",它对我仍然有效。不过,我确实在外部视图中有这行代码。 - Rubberduck
非常感谢。我为这个问题苦苦挣扎了好几个月:https://stackoverflow.com/questions/56509759/bottomsheet-issue-with-nested-scrolling-recyclerview-nestedscrollview。在所有水平RecyclerView上禁用**nestedscrolling**解决了问题! - Mackovich

0

请查看创建折叠效果。这可能会对您有所帮助。

如果链接失效,请参考链接中的代码。

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

像这样为布局管理器添加LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); // 使用线性布局管理器 myUpdatedProductsListRecyclerView.setLayoutManager(mLayoutManager); - Harsha

0

如果有人想知道如何解决内部水平ViewPager2的相同问题,这里是解决方案:

(viewpager.getChildAt(0) as? RecyclerView)?.isNestedScrollingEnabled = false

如果你想知道为什么要使用getChildAt(0),那么ViewPager2是一个包含RecyclerView作为其子项的ViewGroup。因此,直接将isNestedScrollingEnabled = false设置到ViewPager2上是行不通的,因为我们需要针对RecyclerView进行操作。


-5

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

在您的RecyclerView中,将嵌套的ScrollView放入其中,而不是像Scroll behavior in nested RecyclerView with horizontal scroll建议的那样。

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

嗨@Simon,这个应用会崩溃吗?而且你提供的链接中的答案说RecyclerView在NestedScrollView内部,但是你的答案与之相反。 - Rafique Mohammed
尝试了一下,应用程序崩溃了。抛出异常: Caused by: java.lang.IllegalStateException: RecyclerView没有LayoutManager。 - Rafique Mohammed
像这样为布局管理器添加LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);// 使用线性布局管理器 myUpdatedProductsListRecyclerView.setLayoutManager(mLayoutManager); - Harsha

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