AppBarLayout 与 NestedScrollView 的滚动行为不生效

发布于:2025-02-10 ⋅ 阅读:(46) ⋅ 点赞:(0)

问题描述与处理策略

1、问题描述
  • 如下布局文件,AppBarLayout 与 NestedScrollView 的滚动行为不生效
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AppBarLayoutFirstTestActivity"
    tools:ignore="MissingConstraints">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:title="My App" />
    </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">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="This is a scrollable content area."
                android:textSize="18sp" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
2、处理策略
  1. 检查 CoordinatorLayout:AppBarLayout 与 NestedScrollView 的滚动行为依赖于 CoordinatorLayout,确保根布局是 CoordinatorLayout,而不是 ConstraintLayout 或其他布局
<androidx.coordinatorlayout.widget.CoordinatorLayout>
	...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
  1. 检查 NestedScrollView 的 layout_behavior:确保 NestedScrollView 设置了正确的 layout_behavior
app:layout_behavior="@string/appbar_scrolling_view_behavior"
  1. 检查 Toolbar 的 layout_scrollFlags:正确设置了 Toolbar 的 layout_scrollFlags 才能响应滚动行为
app:layout_scrollFlags="scroll|enterAlways|snap"
  • 如下是修改后的布局文件
<?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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AppBarLayoutFirstTestActivity"
    tools:ignore="MissingConstraints">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:title="My App" />
    </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">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="This is a scrollable content area."
                android:textSize="18sp" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

网站公告

今日签到

点亮在社区的每一天
去签到