我试图让我的应用程序中的Toolbar
隐藏和显示基于RecyclerView
的滚动。这个gif显示了我想要实现的目标。
我正在关注this tutorial并没有得到我正在寻找的结果。这是我的活动布局:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:fitsSystemWindows="true">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer" />
</android.support.design.widget.CoordinatorLayout>
这是
Toolbar
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/ColorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
当我运行此代码时,Toolbar
完全消失。怎么了?
如果您的RecyclerView
位于片段内,请尝试将以下代码放在片段布局的根视图中:app:layout_behavior="@string/appbar_scrolling_view_behavior"
。包含它的视图必须是CoordinatorLayout
的直接子项
正如@ orrett3描述的那样只需添加此行
app:layout_behavior="@string/appbar_scrolling_view_behavior"
你的容器FrameLayout
像这样
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
我认为RecyclerView
是这个容器的孩子。
你需要做两个动作: