Hide toolbar smoothly on scrolling RecyclerView?
Currently I have RecyclerView
one which contains some list of items. I am listening to Scroll listener
RecyclerView and if the RecyclerView says 500 at some point it should hide the toolbar and it should stay hidden when it crosses 500+. Likewise, it shows the toolbar when I reach <= 450.
This is the code I have tried so far. The problem is that
- It hides the toolbar, but it blinks when it is hidden or displayed at the specified point.
-
How to achieve a smooth toolbar?
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); scrollD = scrollD + dy; Log.d("key", "DY is .." + (dy + scrollD)); if (scrollD >= 500) { // code to hide } if (scrollD <= 450) { // code to show } } });
source to share
Use CoordinatorLayout instead of Linear / Relative layout and add the following attribute to the toolbar.
app:layout_scrollFlags="scroll|enterAlways"
The Layout Coordinator handles the visibility of the toolbar by hiding it when the user scrolls down and showing it again when the user scrolls up.
Code:
<?xml version="1.0" encoding="utf-8"?>
<!-- $Id$ -->
<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/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Follow this link: https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/
source to share
I was also looking for the same solution and found this. I work well with me.
Hide toolbar
mToolbar.animate().translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
Show toolbar:
mToolbar.animate().translationY(mToolbar.getTop()).setInterpolator(new AccelerateInterpolator()).start();
call these lines in the listener scroll of the recycler view.
Now that the listener gives you the dx and dy values of the toolbar. So, in the above lines of code, instead of mToolbar.getTop (), you can write:
int heightDelta += dy;
bothToolbarLayouts.animate().translationY(-heightDelta).setInterpolator(new AccelerateInterpolator()).start();
Voila, you're done!
Alternatively, to better understand it follow this link
source to share