【问题标题】:Toolbar not hiding on RecyclerView scroll工具栏不隐藏在 RecyclerView 滚动
【发布时间】:2019-02-19 18:43:37
【问题描述】:

我正在尝试根据RecyclerView 的滚动来隐藏和显示我的应用程序中的Toolbar。这个 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 完全消失了。怎么了?

【问题讨论】:

  • 我想学习第 3 部分的教程,因为我很快就会实现一个 FAB。 @SanketKachhela
  • 这里也一样。我的应用程序具有几乎相同的结构并遵循相同的教程,但工具栏不会在滚动时隐藏。如果我能解决这个问题,我会告诉你的
  • 我也有同样的问题。有人解决了吗?
  • @bashan 我设法解决了这个问题,并在一个单独的线程中解决了另一个问题,但我不记得这是多久以前的事了。不过,这可能会有所帮助,请查看stackoverflow.com/a/31241616/3038563

标签: android android-recyclerview android-toolbar


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    正如@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 是这个容器的一个子容器。

    【讨论】:

      【解决方案3】:

      你需要做两个动作:

      1. 从工具栏 xml 中删除这一行:

        app:layout_scrollFlags="scroll|enterAlways"

      2. 作为其他答案,将此行添加到包装片段的布局中 (在你的情况下是框架布局)

        app:layout_behavior="@string/appbar_scrolling_view_behavior"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多