RecyclerView滚动时工具栏未隐藏

7
我正在尝试根据RecyclerView的滚动来隐藏和显示我的应用程序中的Toolbar。这个GIF演示了我想要实现的效果。

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>

这是工具栏布局的代码:
<?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" />

当我运行这段代码时,工具栏完全消失了。出了什么问题?

请查看此链接:https://mzgreen.github.io/2015/02/28/How-to-hideshow-Toolbar-when-list-is-scrolling%28part2%29/ - Sanket Kachhela
我想跟随第三部分的教程,因为我很快就要实现一个FAB。@SanketKachhela - wasimsandhu
我也是。我的应用程序几乎有相同的结构,并且遵循了相同的教程,但是工具栏在滚动时不会隐藏。如果我成功解决了这个问题,我会告诉你的。 - ThanosFisherman
我也遇到了同样的问题。有人解决了吗? - bashan
@bashan 我已经解决了这个问题并在另一个线程中继续处理,但是因为这是一段时间之前的事情,我无法回忆起具体方法。不过这个链接可能会有所帮助,请查看 https://dev59.com/nYzda4cB1Zd3GeqPprP3#31241616 - wasimsandhu
3个回答

2

如果您的RecyclerView位于片段内,请在片段布局的根视图中放置以下代码:app:layout_behavior="@string/appbar_scrolling_view_behavior"。包含该代码的视图必须是CoordinatorLayout的直接子级。


0

你需要执行两个操作:

  1. 从工具栏的XML中删除以下行:

    app:layout_scrollFlags="scroll|enterAlways"

  2. 像其他答案一样,在包含你的片段的布局中(在你的情况下是帧布局),添加以下行:

    app:layout_behavior="@string/appbar_scrolling_view_behavior"


0

就像 @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是这个容器的子元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接