4

I want to show or hide my action bar more smoothly.. Currently i'm doing this where I my scroll state of the recycler view in my activity changes.

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

    }

I want a smoother animation, as in the Google Play app.

Styles.xml

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

    </style>

Initializing the action bar

 setSupportActionBar(mToolbar);
    mActionBar = getSupportActionBar();
Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • I think the right approach here would be to adapt the opacity of the Actionbar in a ScrollView position listener. I think if you try that, you'll stumble on some new problems for a more specific question. – Tim Kranen May 01 '15 at 08:00
  • I don't want to do nothing with the opacity, I just want to scroll up or down the action bar, because I also have some tabs under it. – Boldijar Paul May 01 '15 at 08:04
  • Oh, oops. Sorry, I didn't quite catch that. That's a lot tougher. I'm sorry but I can't help you with that :( – Tim Kranen May 01 '15 at 08:06
  • why do yiu think they are using an ActionBar? – pskink May 01 '15 at 08:08
  • Maybe this [link](http://stackoverflow.com/questions/27931122/how-do-the-animation-hiding-the-actionbar-and-keeping-tabs) will help you – Josef May 01 '15 at 08:08

1 Answers1

3

Use Toolbar from the support library, and scrollable widgets from ObservableScrollview from: https://github.com/ksoichiro/Android-ObservableScrollView

Below is an example implementation that overrides the ObservableScrollViewCallbacks. Note that it also animates the toolbar at the end of the scroll, to avoid having the toolbar only half-visible, which could look a bit weird. Here's a demo video: 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);
    }
}
memoizr
  • 2,081
  • 2
  • 18
  • 24