Toolbar not hiding on RecyclerView scroll
Asked Answered
F

3

7

I'm trying to make the Toolbar in my app hide and show based on the RecyclerView's scrolling. This gif shows what I'm trying to achieve.

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:

<?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. What's wrong?

Furan answered 4/7, 2015 at 7:41 Comment(5)
take a look at this mzgreen.github.io/2015/02/28/…Earthly
I'd like to follow the Part 3 tutorial because I'm going to implement a FAB soon. @SanketKachhelaFuran
Same here. I have almost the same structure in my app and followed the same tutorial but toolbar won't hide on scroll. I'll let you know if i manage to work this outGimble
I have the same issue. Anyone solved it?Hosfmann
@Hosfmann I managed to solve this and move onto another problem in a separate thread, but I can't remember how since this was a while ago. It might help though, check it out https://mcmap.net/q/1627933/-toolbar-leaving-white-space-with-hide-on-recyclerview-scrollFuran
G
2

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". The view that contains that must be a direct child of the CoordinatorLayout

Goatskin answered 4/7, 2015 at 7:49 Comment(0)
H
0

As @orrett3 described just add this line

 app:layout_behavior="@string/appbar_scrolling_view_behavior"

to your container FrameLayout like this

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

Hydrotherapy answered 9/3, 2017 at 23:55 Comment(0)
S
0

You need to do 2 actions:

  1. remove from toolbar xml this line:

    app:layout_scrollFlags="scroll|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"

Sympathetic answered 19/2, 2019 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.