Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android "smoother" hiding of Toolbar when scrolling

I am using a Toolbar inside a ListFragment in Android and can hide/show it during scrolling. I implement AbsListView.OnScrollListener and use this code inside:

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    // Leave this empty
}

/**
 * Method to detect if the scroll status changed
 *
 * @param listView
 * @param scrollState
 */
@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {
    if (!isTablet) {
        final int currentFirstVisibleItem = listView.getFirstVisiblePosition();

        if (currentFirstVisibleItem > mLastFirstVisibleItem) {
            ((ActionBarActivity) getActivity()).getSupportActionBar().hide();
        } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
            ((ActionBarActivity) getActivity()).getSupportActionBar().show();
        }
        mLastFirstVisibleItem = currentFirstVisibleItem;
    }
}

This way the Toolbar is hidden when I scroll down and shown if I scroll upwards. But the animation is very "hard" and I would like to have a smoother transition. It should look like this (without tabs, just the Toolbar): https://cms-images.idgesg.net/images/article/2014/10/playscroll-100509755-large.gif

like image 493
Mokkapps Avatar asked Dec 18 '14 09:12

Mokkapps


1 Answers

I solved the problem using this library: https://github.com/ksoichiro/Android-ObservableScrollView

I modified the ToolbarControlListView example and now I have a smooth animation. Therefore I also had to change the layout file of my ListView which didn't consist of a FrameLayout as parent. Take a look at the examples in GitHub!

like image 177
Mokkapps Avatar answered Nov 01 '22 21:11

Mokkapps