Hiding Toolbar smoothly on RecyclerView Scroll?

会有一股神秘感。 提交于 2019-12-10 14:04:45

问题


Currently I've a RecyclerView that holds some list of items. I'm listening the Scroll listener of RecyclerView and if the RecyclerView at some point say 500, it should hide the toolbar and it should remain hide when it crosses to 500+. Similarly, it shows the toolbar when i reaches <= 450.

This is the code I've tried so far. The problem is,

  1. It hides the toolbar but it flashes when it hides or shows at that mentioned point.
  2. How to achieve a smooth toolbar hide?

    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
    
                }
            }
        });
    

回答1:


Use CoordinatorLayout instead of Linear/Relative layout and add the following attribute to the toolbar.

app:layout_scrollFlags="scroll|enterAlways"

CoordinatorLayout handles visibility of 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>

Refer this link : https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/




回答2:


I was also searching for the same solution and found this. Working fine with me.

TO hide a Toolbar

mToolbar.animate().translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();

To Show the Toolbar:

mToolbar.animate().translationY(mToolbar.getTop()).setInterpolator(new AccelerateInterpolator()).start();

call those lines on recycler view's scroll listener.

Now since listener gives you dx and dy values of Toolbar. So in above lines of code, instead of mToolbar.getTop() You can write:

int heightDelta += dy;
    bothToolbarLayouts.animate().translationY(-heightDelta).setInterpolator(new AccelerateInterpolator()).start();

Voila you are done!

Alternatively to understand it more better follow this link



来源:https://stackoverflow.com/questions/30243640/hiding-toolbar-smoothly-on-recyclerview-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!