[英]Toolbar not hiding on RecyclerView scroll
我正在尝试使我的应用程序中的Toolbar
隐藏并显示基于RecyclerView
的滚动。 这个gif显示了我想要实现的目标。
我正在按照本教程而不是得到我正在寻找的结果。 这是我的活动布局:
<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
是这个容器的孩子。
你需要做两个动作:
从工具栏中删除xml这一行:
应用:layout_scrollFlags = “滚动| enterAlways”
作为其他答案,将此行添加到包裹片段的布局(在您的情况下是它的框架布局)
应用程式:layout_behavior = “@串/ appbar_scrolling_view_behavior”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.