如何实现类似WhatsApp的带TabLayout的折叠Toolbar效果
实现滚动时Toolbar隐藏、TabLayout固定的效果
我想要实现WhatsApp首页的滚动交互:向上滚动页面时,顶部Toolbar消失,仅TabLayout保留在顶部。尝试了多种方法都没达到预期效果,以下是我当前的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" tools:context=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/collapsingToolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textSize="22sp" android:textColor="@color/black" /> </androidx.appcompat.widget.Toolbar> </com.google.android.material.appbar.CollapsingToolbarLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_collapseMode="pin" /> </com.google.android.material.appbar.AppBarLayout> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
修正后的布局代码
<?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" tools:context=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/collapsingToolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|enterAlwaysCollapsed"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="parallax"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textSize="22sp" android:textColor="@color/black" /> </androidx.appcompat.widget.Toolbar> </com.google.android.material.appbar.CollapsingToolbarLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|enterAlways" /> </com.google.android.material.appbar.AppBarLayout> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
核心修改点说明
- 滚动标志调整:
- 给
CollapsingToolbarLayout设置scroll|enterAlwaysCollapsed,让它随页面向上滚动完全隐藏,向下滚动时重新显示。 - 给
TabLayout设置scroll|enterAlways,确保它始终固定在顶部,不随滚动消失。
- 给
- 布局高度规范:
- 将
Toolbar高度设为?attr/actionBarSize,使用系统标准ActionBar高度,避免高度不确定导致的滚动异常。 - 把
CollapsingToolbarLayout的高度改为wrap_content,适配内部Toolbar的高度,无需占满整个AppBarLayout。
- 将
- Behavior优化:
- ViewPager2的
layout_behavior改用@string/appbar_scrolling_view_behavior替代硬编码类路径,符合Material Design规范。
- ViewPager2的
额外注意事项
确保ViewPager2加载的Fragment中,滚动控件(如RecyclerView)已开启嵌套滚动:
<androidx.recyclerview.widget.RecyclerView ... android:nestedScrollingEnabled="true" />
只有开启嵌套滚动,才能触发AppBarLayout的滚动联动效果。
内容的提问来源于stack exchange,提问作者Hrithik Rajput
相关产品推荐
相关产品推荐

