Hiding Toolbar on scroll with recyclerview inside fragment

21,244

Solution 1

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/tabanim_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tabanim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

Solution 2

You can fix this by adding a third flag - snap, to layout_scrollFlags property.

<Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="5dp"
    app:layout_scrollFlags="scroll|enterAlways|snap" />

Using this option will determine what to do when a view only has been partially reduced. If scrolling ends and the view size has been reduced to less than 50% of its original, then this view to return to its original size. If the size is greater than 50% of its sized, it will disappear completely.

check this link.

Solution 3

Haven't tested this yet, but this is what you must do:

  • Move the app:layout_behavior="@string/appbar_scrolling_view_behavior" to the FrameLayout with id container.
  • Add android:fitsSystemWindows = "true" to your AppBarLayout.

This is exactly what I'm doing in my app, and it works. If this doesn't work for you, try cleaning (on Android Studio, Build -> Clean Project) your project and running the app again.

Solution 4

Make one layout name as toolbar.xml and include it in your code where you want to use, but remind one thing please do not use any other layout like Relativelayout, Linearlayout and other on above of

Solution 5

You need something similar to the play store app. Pasting the layouts here. The code for this is easy and you can code it by yourself.

  1. FrameLayout
    • ImageView
    • ScrollView
      1. LinearLayout
      2. HorizontalScrollView
        • LinearLayout
    • ToolBar

With the above layout hierarchy you can achieve something like this:

enter image description here

Share:
21,244
Orbit
Author by

Orbit

Updated on February 04, 2020

Comments

  • Orbit
    Orbit over 4 years

    I'm trying to get the toolbar to collapse on scroll when a recyclerview inside a fragment is scrolled. To start, heres my main layout:

    <DrawerLayout>
    
         <RelativeLayout
            android:id="@+id/mainRelativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    
            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"  
                >
    
                <android.support.design.widget.AppBarLayout
                    android:id="@+id/appBarLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
    
                    <Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:elevation="5dp"
                        app:layout_scrollFlags="scroll|enterAlways"
                        >
    
                    </Toolbar>
    
                </android.support.design.widget.AppBarLayout>
    
                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    />
    
            </android.support.design.widget.CoordinatorLayout>
    
        </RelativeLayout>
    
    <!-- ignore -->
    <drawercontents>
    </DrawerLayout>
    

    So as you can probably guess my fragments are being loaded into @id/container. My first fragment contains the recyclerview, and I set app:layout_behavior="@string/appbar_scrolling_view_behavior" on that recyclerview. This does work, and the toolbar collapses on scroll. The issue is the toolbar covers the top contents of the fragment when its not collapsed. Adding a top margin to the fragment container equal to the size of the toolbar just causes a blank space to be left when the toolbar collapses (obviously).

    Whats missing here? Any ideas?

    EDIT: As requested, here is the layout for the fragment containing the recyclerview:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/feed"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00000000"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>