Hide / Show action bar more smoothly?

I want to show or hide my action bar more smoothly .. Currently, I do this when my scrolller view scrolls state in my activity changes.

 if (scrollState == ScrollState.UP) {
        if (mActionBar.isShowing()) {
            mActionBar.hide();
        }
    } else if (scrollState == ScrollState.DOWN) {
        if (!mActionBar.isShowing()) {
            mActionBar.show();
        }

    }

I need smoother animations like in the Google Play app.

styles.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>

    </style>

Initialize the action bar

 setSupportActionBar(mToolbar);
    mActionBar = getSupportActionBar();
+4
source share
1 answer

Use Toolbarfrom the support library and scrollable widgets from ObservableScrollviewfrom: https://github.com/ksoichiro/Android-ObservableScrollView

, ObservableScrollViewCallbacks. , , , . : https://drive.google.com/file/d/0B7TH7VeIpgSQa293YmhSY1M2Um8/view?usp=sharing

@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {

    toolbar.animate().cancel();

    int scrollDelta = scrollY - oldScrollY;
    oldScrollY = scrollY;

    float currentYTranslation = -toolbar.getTranslationY();
    float targetYTranslation = Math.min(Math.max(currentYTranslation + scrollDelta, 0), toolbarHeight);
    toolbar.setTranslationY(-targetYTranslation);
}

@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
    float currentYTranslation = -toolbar.getTranslationY();
    int currentScroll = listView.getCurrentScrollY();

    if (currentScroll < toolbarHeight) {
        toolbar.animate().translationY(0);
    } else if (currentYTranslation > toolbarHeight /2) {
        toolbar.animate().translationY(-toolbarHeight);
    } else {
        toolbar.animate().translationY(0);
    }
}
+3

All Articles