You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Material 3中AppBarLayout与SwipeRefreshLayout滚动交互异常问题

问题描述

我正在开发一款采用Material 3(Material You)设计、具备下拉刷新功能的应用。当前遇到问题:滚动SwipeRefreshLayout下的内容时,AppBarLayout无法正常抬升,但下拉刷新状态下该功能正常。已尝试将SwipeRefreshLayout的AndroidX依赖版本从1.2.0-alpha01切换至1.1.0,问题仍未解决。期望实现滚动内容时AppBarLayout正常抬升的效果。

解决方案

问题根源在于SwipeRefreshLayout和内部NestedScrollView的布局参数未正确配置,导致滚动事件无法传递给CoordinatorLayout,使得AppBarLayout无法感知内部滚动动作。需要做以下两处调整:

  • 将SwipeRefreshLayout的android:layout_height从wrap_content改为match_parent,确保它填充整个可用空间,正确接收滚动事件。
  • 给内部的NestedScrollView添加android:fillViewport="true"属性,并将其android:layout_height设为match_parent,让它填满父容器,保证滚动行为能被系统正确检测。

修改后的activity-main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"

    android:fitsSystemWindows="true"

    tools:context=".MainActivity">

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

        android:fitsSystemWindows="true"
        app:liftOnScroll="true">

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

            app:title="App Bar and Swipe Layout"
            app:titleCentered="true"/>

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

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:text="Long lorem ipsum text..."/>

        </androidx.core.widget.NestedScrollView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

内容的提问来源于stack exchange,提问作者FOUR BITS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 11:17:08