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

如何通过AppBarLayout实现Toolbar与Child A常驻、Child B随RecyclerView滚动隐藏?

解决方案:无需CollapsingToolbar,仅用AppBarLayout即可实现

嘿,这个需求根本不需要用到CollapsingToolbarLayout,只靠AppBarLayout加上正确的layout_scrollFlags配置就能轻松搞定!下面给你详细说清楚怎么做:

核心思路

AppBarLayout的子View通过app:layout_scrollFlags属性控制滚动行为:

  • 给需要随滚动隐藏的Child B设置滚动标识,让它参与滚动流程
  • 让Toolbar和Child A不参与滚动,从而始终固定在顶部

完整布局代码示例

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <!-- Toolbar:始终可见,不设置scrollFlags -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

        <!-- Child A:始终可见,不设置scrollFlags -->
        <LinearLayout
            android:id="@+id/child_a"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:background="#eee">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Child A - 始终可见"/>
        </LinearLayout>

        <!-- Child B:随滚动隐藏,设置scrollFlags="scroll" -->
        <LinearLayout
            android:id="@+id/child_b"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:padding="16dp"
            android:background="#ddd"
            app:layout_scrollFlags="scroll">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Child B - 随滚动隐藏"/>
        </LinearLayout>

    </android.support.design.widget.AppBarLayout>

    <!-- RecyclerView:必须设置behavior才能和AppBarLayout联动 -->
    <android.support.v7.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"/>

</android.support.design.widget.CoordinatorLayout>

关键细节说明

  1. CoordinatorLayout是基础:它负责协调AppBarLayout和RecyclerView的滚动联动,必须作为最外层布局
  2. Toolbar和Child A:没有设置app:layout_scrollFlags,所以它们不会参与滚动,始终固定在AppBarLayout顶部
  3. Child B的滚动配置:
    • 用scroll标识:当RecyclerView向上滚动时,Child B会被完全滑出屏幕;向下滚动时重新显示
    • 如果想让Child B先收缩到最小高度再隐藏,可以改成scroll|exitUntilCollapsed,同时给Child B设置android:minHeight属性(比如40dp)
  4. RecyclerView的behavior:必须设置app:layout_behavior="@string/appbar_scrolling_view_behavior",这样它才能触发AppBarLayout的滚动行为

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:43:10