[英]Hide / Show actionbar more smoothly?
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. 我想要一个更流畅的动画,就像在Google Play应用中一样。
Styles.xml Styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
</style>
Initializing the action bar 初始化操作栏
setSupportActionBar(mToolbar);
mActionBar = getSupportActionBar();
Use Toolbar
from the support library, and scrollable widgets from ObservableScrollview
from: https://github.com/ksoichiro/Android-ObservableScrollView 使用支持库中的Toolbar
和来自ObservableScrollview
可滚动窗口小部件: https : //github.com/ksoichiro/Android-ObservableScrollView
Below is an example implementation that overrides the ObservableScrollViewCallbacks
. 下面是一个覆盖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 这是一个演示视频: 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.