Android "smoother" hiding of Toolbar when scrolling
Asked Answered
F

1

6

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

Frye answered 18/12, 2014 at 9:4 Comment(0)
F
7

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!

Frye answered 18/12, 2014 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.