Rhimi Wajih
Rhimi Wajih

Reputation: 1

How to make hide/show toolbar when our list scrolling to the top

How to make hide/show toolbar when our list scrolling to the top, knowing that the toolbar view is described inside activity_main.xml but recyclerView is described in another fragmet nomed Fragment_main.xml

sorry for my english :)

Upvotes: 0

Views: 139

Answers (3)

Asif Patel
Asif Patel

Reputation: 1764

Use below code:

<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
    ...>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
       <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"/>

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

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

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

If RecyclerView is in another fragment, then add below line to the View which contain RecyclerView in this CoordinatorLayout.

app:layout_behavior="@string/appbar_scrolling_view_behavior"

Must Use CoordinatorLayout.

Upvotes: 0

Lucas Arrefelt
Lucas Arrefelt

Reputation: 3929

Since your activity which has the toolbar within its content view is starting the fragment, you can always get a hold of it from your fragment.

MainActivity mainActivity = (MainActivity)getActivity();

I would recommend doing a method for it in your MainActivity:

public void showToolbar(boolean show) {

    // If you have your toolbar as a private member of MainActivity
    toolbar.setVisiblity(show ? View.VISIBLE : View.GONE);

    // But you can also do this
    if (show) {
        getSupportActionBar().show();
    }
    else {
        getSupportActionBar().hide();
    }
}

And then when you actually want to hide it from your fragment, call it:

((MainActivity)getActivity()).showToolbar(false);

To make the UI change more smooth, I recommend translating it instead of just instantly hiding it. Take a look at the top answer here for inspiration:

android lollipop toolbar: how to hide/show the toolbar while scrolling?

If you don't know how to take care of when to actually show or hide it via scroll logic, take a look at this library which handles a lot for you and also gives examples:

https://github.com/ksoichiro/Android-ObservableScrollView

Upvotes: 1

Adley
Adley

Reputation: 541

This is kind of simple. Just put this code in your toolbar

 <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"/>

This will do the magic : app:layout_scrollFlags="scroll|enterAlways"

remember to add in your xml the app call xmlns:app="http://schemas.android.com/apk/res-auto"

Check this tutorial: https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

Upvotes: 0

Related Questions