首页 > 解决方案 > 禁用扩展时自动滚动 AppBarLayout

问题描述

如果用户向下滚动列表一点点,我有一个 AppBarLayout 会展开,如果用户不再滚动,它会在 2 秒后恢复。然而,令人讨厌的是 AppBarLayout 也在滚动 RecyclerView 以获取 AppBarLayout 的高度。这样,它将列表保持在用户在列表中停止的相同位置,因此列表不会移动,但是,视口会移动,如果可能的话,我想禁用它。

它起作用的唯一方法是当我将 ViewPager(稍后在包含 RecyclerView 的 Fragment 中)放在 AppBarLayout 上方并移除时app:layout_behavior

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/divider_logo">

   <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        app:elevation="0dp"
        app:layout_behavior="com.my.app.sava.main.AppBarLayoutBehavior">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

            <View
                android:id="@+id/tv_location_bg"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/colorPrimaryLight"
                app:layout_constraintBottom_toBottomOf="@id/tv_location"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@id/tv_location" />
               .
               .
               .

然而,当想要滚动时会有延迟,因为 AppBarLayout 首先滚动而不移动列表,然后 RecyclerView 开始滚动。

标签: androidkotlinandroid-recyclerviewandroid-appbarlayout

解决方案


推荐阅读