在垂直RecyclerView内的水平RecyclerView上垂直滚动时,AppBarLayout不会折叠/展开

问题描述

当垂直RecyclerView具有水平RecyclerView时,如果用户开始用手指在水平RecyclerView上垂直滚动,则AppBarLayout完全不会移动。如果手指在其他列表项上,则AppBarLayout确实会更改其状态。

Collapsibletoolbar代码摘自this文章

AppBarLayout中的CollapsingToolbarLayout仍然存在问题。

我的代码有问题吗?还是库错误

Example

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="76dp"
        android:theme="@style/AppTheme.AppBarOverlay.Light">

        <com.example.notcollapsingappbar.Collapsibletoolbar
            android:id="@+id/motion_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:minHeight="@dimen/actionBarSize"
            app:layoutDescription="@xml/scene_header"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="start|center_vertical"
                android:text="Large title"
                android:textColor="@android:color/white"
                android:textSize="30sp"
                app:autoSizeTextType="uniform" />

        </com.example.notcollapsingappbar.Collapsibletoolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="16dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

解决方法

您可以将此行添加到水平RecyclerView

android:nestedScrollingEnabled="false"

或以编程方式:

recyclerView.setNestedScrollingEnabled(false);
,

@potados的答案解决了这个问题。但是没有解释它为什么起作用。

我在issue的GitHub页面上创建了一个material components android,并提出了相同的问题。我想在这里发布我收到的答案。

尝试将Horizo​​ntal RecyclerView的nestedScrollEnable设置为false

如果启用NestedScrollingChild作为子视图,则NestedScrollingParent作为父布局将不会拦截滚动事件