隐藏/显示操作栏更流畅?

Hide / Show actionbar more smoothly?

我想更流畅地显示或隐藏我的操作栏.. 目前,我正在执行此操作,我 activity 更改了回收站视图的滚动状态。

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

    }

我想要更流畅的动画,就像在 Google Play 应用程序中一样。

Styles.xml

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

    </style>

正在初始化操作栏

 setSupportActionBar(mToolbar);
    mActionBar = getSupportActionBar();

使用支持库中的 Toolbar,以及来自 ObservableScrollview 的可滚动小部件: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);
    }
}