简体   繁体   English

工具栏没有隐藏在RecyclerView滚动上

[英]Toolbar not hiding on RecyclerView scroll

I'm trying to make the Toolbar in my app hide and show based on the RecyclerView 's scrolling. 我正在尝试使我的应用程序中的Toolbar隐藏并显示基于RecyclerView的滚动。 This gif shows what I'm trying to achieve. 这个gif显示了我想要实现的目标。

GIF

I'm following this tutorial and not getting the results I'm looking for. 我正在按照本教程而不是得到我正在寻找的结果。 Here is my activity's layout: 这是我的活动布局:

<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>

And here's the Toolbar layout: 这是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" />

When I run this code, the Toolbar completely disappears. 当我运行此代码时, Toolbar完全消失。 What's wrong? 怎么了?

If your RecyclerView is inside of a fragment try putting the following code in the root view of the fragment layout: app:layout_behavior="@string/appbar_scrolling_view_behavior" . 如果您的RecyclerView位于片段内,请尝试将以下代码放在片段布局的根视图中: app:layout_behavior="@string/appbar_scrolling_view_behavior" The view that contains that must be a direct child of the CoordinatorLayout 包含它的视图必须是CoordinatorLayout的直接子项

As @orrett3 described just add this line 正如@ orrett3描述的那样只需添加此行

 app:layout_behavior="@string/appbar_scrolling_view_behavior"

to your container FrameLayout like this 你的容器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"/>

I assumed the RecyclerView is a child of this container. 我假设RecyclerView是这个容器的孩子。

You need to do 2 actions: 你需要做两个动作:

  1. remove from toolbar xml this line: 从工具栏中删除xml这一行:

    app:layout_scrollFlags="scroll|enterAlways" 应用:layout_scrollFlags = “滚动| enterAlways”

  2. As other answers, add this line to layout that wrap your fragment's (in your case it's frame layout) 作为其他答案,将此行添加到包裹片段的布局(在您的情况下是它的框架布局)

    app:layout_behavior="@string/appbar_scrolling_view_behavior" 应用程式:layout_behavior = “@串/ appbar_scrolling_view_behavior”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM