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

Android实现RecyclerView滚动时View B先折叠后View A联动滚动的方案

实现类似iPhone设置页面的滚动折叠效果

核心思路

通过CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout的组合,给View A和View B配置不同的滚动行为,实现View B先折叠隐藏、之后View A再跟随滚动的效果。

布局代码示例

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

    <!-- AppBar容器:包含View A和View B -->
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:collapsedHeight="56dp"> <!-- 折叠后高度设为View A的高度 -->

            <!-- View B:先折叠的搜索栏区域 -->
            <LinearLayout
                android:id="@+id/view_b"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="#f0f0f0"
                android:gravity="center"
                app:layout_collapseMode="parallax">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="View B(搜索栏)"/>
            </LinearLayout>

            <!-- View A:固定顶部的工具栏 -->
            <LinearLayout
                android:id="@+id/view_a"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:background="#2196F3"
                android:gravity="center"
                app:layout_collapseMode="pin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="View A(工具栏)"
                    android:textColor="#ffffff"/>
            </LinearLayout>

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

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

    <!-- 触发滚动的RecyclerView -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

关键配置说明

  • CollapsingToolbarLayout的scrollFlags设为scroll|exitUntilCollapsed:RecyclerView滚动时,该布局会跟随滚动,直到折叠到collapsedHeight指定的高度(即View A的高度)。
  • View B的collapseMode设为parallax:折叠时产生视差渐变效果,若不需要渐变可改为none实现直接隐藏。
  • View A的collapseMode设为pin:在View B折叠过程中始终固定在顶部,等View B完全隐藏后,才会随CollapsingToolbarLayout一起向上滚动消失。
  • RecyclerView的layout_behavior设为@string/appbar_scrolling_view_behavior:确保滚动事件能传递给AppBarLayout,触发整个折叠逻辑。

内容的提问来源于stack exchange,提问作者Divij Gupta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 05:00:03