To make the toolbar expand and smooth smoothly, you can apply translation animations or use CoordinatorLayout using the AppBarLayout and toolbar.
Animation: First you have to detect a scroll up and scroll down your RecyclerView. To do this, you can set "setOnScrollListener" in the RecyclerView. When you have scroll up and scroll down, just apply the animation.
The code:
rvHomeList.setOnScrollListener(new RecyclerView.OnScrollListener() { int verticalOffset; boolean scrollingUp; @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { if (newState == RecyclerView.SCROLL_STATE_IDLE) { if (scrollingUp) { Log.e("onScrollStateChanged", "UP"); if (verticalOffset > llTop.getHeight()) { toolbarAnimateHide(); } } else { Log.e("onScrollStateChanged", "down"); if (llTop.getTranslationY() < llTop.getHeight() * -0.6 && verticalOffset > llTop.getHeight()) { toolbarAnimateHide(); } else { toolbarAnimateShow(verticalOffset); } } } } @Override public final void onScrolled(RecyclerView recyclerView, int dx, int dy) { verticalOffset += dy; scrollingUp = dy > 0; int toolbarYOffset = (int) (dy - llTop.getTranslationY()); llTop.animate().cancel(); if (scrollingUp) { Log.e("onScrolled", "UP"); if (toolbarYOffset < llTop.getHeight()) { llTop.setTranslationY(-toolbarYOffset); } else { llTop.setTranslationY(-llTop.getHeight()); } } else { Log.e("onScrolled", "down"); if (toolbarYOffset < 0) { llTop.setTranslationY(0); } else { llTop.setTranslationY(-toolbarYOffset); } } } });
Animation Methods:
private void toolbarAnimateShow(final int verticalOffset) { if (!isShowing) { isShowing = true; llTop.animate() .translationY(0) .setInterpolator(new LinearInterpolator()) .setDuration(180) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { llTop.setVisibility(View.VISIBLE); isShowing = false; } }); } } private void toolbarAnimateHide() { if (!isHidding) { isHidding = true; llTop.animate() .translationY(-llTop.getHeight()) .setInterpolator(new LinearInterpolator()) .setDuration(180) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { llTop.setVisibility(View.GONE); isHidding = false; } }); } }
Coordinator Layout with AppBarLayout and toolbar:. Using the Layout coordinator using appBarLayout and the toolbar and set the scroll flag used in the attribute application: layout_scrollFlags to achieve the scroll effect. It must be enabled for any scroll effects that will work. This flag must be enabled along with enterAlways, enterAlwaysCollapsed, exitUntilCollapsed, or a snap-in.
- enterAlways: The view will be displayed as you scroll up. This flag is useful when scrolling from the bottom of the list and you want to open the toolbar as soon as it scrolls.
- enterAlwaysCollapsed: Normally, when only enterAlways is used, the toolbar continues to expand as it scrolls down. The enterAlways declaration is declared, and you specified minHeight, you can also specify enterAlwaysCollapsed. When this option is used, your view will only be displayed at that minimum height. Only when the scroll reaches the top does the view expand to its full height.
- exitUntilCollapsed: When the scroll flag is set, scrolling down usually will move all content. By specifying minHeight and exitUntilCollapsed, the minimum height of the toolbar will be reached until the rest of the content starts to scroll and exit the screen.
- binding:. Using this option determines what to do if the view is only partially reduced. If the scroll ends and the size of the view is reduced to less than 50% of its original, then this view will revert to its original size. If the size exceeds 50% of its size, it will completely disappear.
The code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/llBase" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <android.support.design.widget.AppBarLayout android:id="@+id/appbarContainer" 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/toolbar" android:layout_width="match_parent" android:layout_height="@dimen/_40sdp" android:gravity="center" android:theme="@style/ThemeOverlay.AppCompat.Light" app:layout_scrollFlags="scroll|enterAlways"> <include layout="@layout/top_bar" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <RelativeLayout android:id="@+id/rlMain" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </RelativeLayout> </android.support.design.widget.CoordinatorLayout> </LinearLayout>
source share