【问题标题】:Hide / Show actionbar more smoothly?更流畅地隐藏/显示操作栏?
【发布时间】:2015-05-01 07:48:41
【问题描述】:

我想更流畅地显示或隐藏我的操作栏.. 目前我正在这样做,我的活动中回收器视图的滚动状态发生了变化。

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

    }

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

样式.xml

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

    </style>

初始化操作栏

 setSupportActionBar(mToolbar);
    mActionBar = getSupportActionBar();

【问题讨论】:

  • 我认为这里的正确方法是在 ScrollView 位置侦听器中调整 Actionbar 的不透明度。我认为,如果您尝试这样做,您会偶然发现一些针对更具体问题的新问题。
  • 我不想对不透明度做任何事情,我只想向上或向下滚动操作栏,因为它下面还有一些选项卡。
  • 哦,哎呀。抱歉,我没完全听懂。这要困难得多。对不起,我帮不了你:(
  • 为什么 yiu 认为他们在使用 ActionBar?
  • 也许这个link 会帮助你

标签: android animation scroll android-actionbar


【解决方案1】:

使用支持库中的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);
    }
}

【讨论】:

    猜你喜欢
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    相关资源
    最近更新 更多