CollapsingToolbarLayout 未按预期工作

CollapsingToolbarLayout not working as intended

我正在尝试创建一个带有复杂折叠工具栏的屏幕。这是它的布局

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/nested_scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".view.main.profile.ProfileFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/lighter_gradient_background">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/cl_user_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/iv_profile_picture"
                    android:layout_width="@dimen/profile_image_size"
                    android:layout_height="@dimen/profile_image_size"
                    android:layout_marginStart="30dp"
                    android:layout_marginTop="20dp"
                    android:adjustViewBounds="true"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:ignore="ContentDescription" />

                <TextView
                    android:id="@+id/tv_place"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:fontFamily="@font/montserrat_medium"
                    android:paddingBottom="20dp"
                    android:text="@string/location"
                    android:textColor="@color/white"
                    android:textSize="14sp"
                    app:layout_constraintEnd_toEndOf="@id/iv_profile_picture"
                    app:layout_constraintStart_toStartOf="@id/iv_profile_picture"
                    app:layout_constraintTop_toBottomOf="@id/iv_profile_picture" />

                <TextView
                    android:id="@+id/tv_followers"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="80dp"
                    android:fontFamily="@font/montserrat_medium"
                    android:text="321"
                    android:textColor="@color/white"
                    android:textSize="14sp"
                    app:layout_constraintStart_toEndOf="@id/iv_profile_picture"
                    app:layout_constraintTop_toTopOf="@id/iv_profile_picture" />

                <TextView
                    android:id="@+id/tv_followers_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:fontFamily="@font/montserrat_regular"
                    android:text="@string/followers"
                    android:textColor="@color/white"
                    android:textSize="14sp"
                    app:layout_constraintEnd_toEndOf="@id/tv_followers"
                    app:layout_constraintStart_toStartOf="@id/tv_followers"
                    app:layout_constraintTop_toBottomOf="@id/tv_followers" />

                <TextView
                    android:id="@+id/tv_following"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="100dp"
                    android:fontFamily="@font/montserrat_medium"
                    android:text="123"
                    android:textColor="@color/white"
                    android:textSize="14sp"
                    app:layout_constraintStart_toEndOf="@id/tv_followers"
                    app:layout_constraintTop_toTopOf="@id/tv_followers" />

                <TextView
                    android:id="@+id/tv_following_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/montserrat_regular"
                    android:text="@string/following"
                    android:textColor="@color/white"
                    android:textSize="14sp"
                    app:layout_constraintEnd_toEndOf="@id/tv_following"
                    app:layout_constraintStart_toStartOf="@id/tv_following"
                    app:layout_constraintTop_toTopOf="@id/tv_followers_label" />

                <Button
                    android:id="@+id/btn_edit_profile"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginTop="16dp"
                    android:background="@drawable/edit_profile_button"
                    android:fontFamily="@font/poppins_medium"
                    android:text="@string/edit_profile"
                    android:textColor="@color/white"
                    android:textAllCaps="false"
                    android:textSize="12sp"
                    app:layout_constraintEnd_toEndOf="@id/tv_following_label"
                    app:layout_constraintStart_toStartOf="@id/tv_followers_label"
                    app:layout_constraintTop_toBottomOf="@id/tv_followers_label" />

            </androidx.constraintlayout.widget.ConstraintLayout>
        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <TextView
            android:id="@+id/tv_username"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="@font/montserrat_semibold"
            android:text="@string/username"
            android:textColor="@color/white"
            android:textSize="18sp"/>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tl_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/tab_layout_bottom_border"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/app_bar">

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:text="@string/activity" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/interests" />

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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/cl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_posts"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                />

            <View
                android:id="@+id/interests_background"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="@color/white"
                android:visibility="gone"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <Button
                android:id="@+id/btn_add_new_interests"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="16dp"
                android:background="@drawable/add_interests_profile_button"
                android:fontFamily="@font/montserrat_medium"
                android:text="@string/add_new_interests"
                android:textAllCaps="false"
                android:textColor="@color/colorPrimary"
                android:textSize="15sp"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/cg_interests"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:visibility="gone"
                app:chipSpacingVertical="5dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/btn_add_new_interests" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我想要实现的行为是当用户按下 RecyclerView 所在的屏幕部分并将其向上拖动时,工具栏应该折叠。现在发生的行为是当用户执行上述操作时没有任何反应,但是当他按下并轻弹 RecyclerView 时,工具栏才会折叠。有谁知道为什么会这样?如何修复?

我从教程中复制了说明,布局是一样的。这也是在应用程序的另一部分实现的并且运行良好,但该片段有一个更小的更简单的 header,它没有嵌套视图。

在 RecyclerView 上使用 android:nestedScrollingEnabled="false" 禁用嵌套滚动视图后,布局具有预期的行为