[英]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.