0

I've some troubles trying to hide toolbar when user scrolls over the recyclerView.

The toolbar is transparent and is over the recyclerView (through FrameLayout). I've searched a lot but I haven't found any solution to solve this incorrect behaviour.

Currently, I've this xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:statusBarBackground="@android:color/transparent">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true">

        <include layout="@layout/toolbar_activity" />

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

    </FrameLayout>

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

With this code, the toolbar is fixed at the top and it's not affected by app:layout_behavior="@string/appbar_scrolling_view_behavior". I've tried moving that attribute to the FrameLayout but in this case, the recyclerview is below the toolbar, not behind it.

Any idea of how can I solve this? I'm going crazy...

adlagar
  • 877
  • 10
  • 31

3 Answers3

0

set property
app:layout_scrollFlags="scroll|enterAlways"
to child view of android.support.design.widget.AppBarLayout

0

Create a custom class and then extend RecyclerView.OnScrollListener

public class ScrollListener extends RecyclerView.OnScrollListener {
    public ScrollListener() {
    }

    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        switch (newState) {
            case RecyclerView.SCROLL_STATE_IDLE:
                System.out.println("The RecyclerView is not scrolling");
                break;
            case RecyclerView.SCROLL_STATE_DRAGGING:
                System.out.println("Scrolling now");
                break;
            case RecyclerView.SCROLL_STATE_SETTLING:
                System.out.println("Scroll Settling");
                break;

        }

    }

    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

        if (dy > 0) {
            //scrolling downwards: hide/show toolbar 
            System.out.println("Scrolled Downwards");
        } else if (dy < 0) {
            //scrolling downwards: hide/show  toolbar 
        }
    }
}

Attach listener to recycler view

 mRecyclerView.addOnScrollListener(new ScrollListener());
Suraj Bahadur
  • 3,730
  • 3
  • 27
  • 55
0

Add these scroll flags to child of your app bar layout

app:layout_scrollFlags="scroll|enterAlways|snap"

Vishal Arora
  • 2,524
  • 2
  • 11
  • 14