如何使用新的设计库 API 正确隐藏和显示操作栏? [英] How to properly hide&show the actionbar using the new design library API?

本文介绍了如何使用新的设计库 API 正确隐藏和显示操作栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在活动中有这些元素:

Suppose I have these elements in the activity:

  • actionbar(实际上是一个工具栏)
  • 标签(使用 TabLayout )
  • ViewPager,带有片段.
  • 每个片段都有 ListVIew/RecyclerView.

我希望拥有与许多应用程序相同的行为,即在滚动时,操作栏将隐藏或显示.

I wish to have the same behavior as on many apps, that when scrolling, the action bar will hide or show.

一个完美的例子是 Play 商店滚动的工作原理.

A perfect example for it is how the Play Store scrolling works.

这样的东西(当然在片段中有 ListView):

Something like this (but with ListView in the fragments of course) :

我找到了Android-ObservableScrollView"库它显示了如何为多种类型的滚动视图执行此操作,但它没有处理新的 TabLayout 使用情况(加上它有问题),如较新的示例所示,例如 cheesesquare.

在设计库的新 API(如cheesesquare"示例中所示)上,它更加自动化,因此您无需创建太多样板来处理滚动以及如何处理它.

On the new API of the design library (shown on the "cheesesquare" example) , it's more automatic so you don't need to create so much boilerplate for handling the scrolling and what to do with it.

类似的东西:

<android.support.v4.widget.DrawerLayout

  <android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

      <android.support.v7.widget.Toolbar
       app:layout_scrollFlags="scroll|enterAlways"/>

      <android.support.design.widget.TabLayout/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
     app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton/>

  </android.support.design.widget.CoordinatorLayout>

  <android.support.design.widget.NavigationView/>

</android.support.v4.widget.DrawerLayout>

这个承诺承诺小吃店将在 FAB 下方,并且操作栏将根据片段的内部 RecyclerView 自动动画(显示/隐藏).

This hirerchy promises that snackbars will be below the FABs, and that the action bar will automatically be animated (shown/hidden) based on the inner RecyclerView of the fragments.

问题是,这似乎只适用于 RecyclerView,而不适用于 ListView.

Thing is, this seems to work only with RecyclerView, and not with ListView.

由于它们非常复杂,因此很难将它们转换为 RecyclverViews.

Since they are quite complex, it will be very hard to convert them to RecyclverViews.

如何才能使上述机制也适用于 ListView?

What would it take to make the above mechanism work for ListViews too?

有办法吗?如果是,怎么办?

Is there a way to do it? If so, how?

遗憾的是,Google 似乎不支持 ListView 的这种行为,而只支持 RecyclerView 和 NestedScrollView(写的是 此处).

sadly it seems that Google will not support this behavior for ListView, but only for RecyclerView and NestedScrollView (written about here).

所以我尝试使用Android-ObservableScrollView" 库,但它有很多滚动问题,包括投掷问题.

So I've tried to use the "Android-ObservableScrollView" library, but it has a lot of scrolling issues, including fling issues.

现在我决定给 Android-ParallaxHeaderViewPager .谁能告诉我如何使它在当前场景中工作?甚至有可能吗?

Now I've decided to give Android-ParallaxHeaderViewPager . Can anyone please tell me how to make it work in the current scenario? Is it even possible?

毕竟我决定转换为 RecyclerView.但是,我尝试过的所有示例都显示了一个问题:当停止滚动时,操作栏(甚至可能是 FAB)可能会被截断,而不是在 Play 商店中,它会被动画化为隐藏/显示.

I've decided to convert to RecyclerView after all. However, all the samples I've tried show an issue: when stopping to scroll, the actionbar (and maybe even the FAB) can stay truncated, as opposed to the play store, where it will get animated to either being hidden/shown.

当 onScrollStateChanged 处于空闲状态时,我似乎需要使用 setExpanded .问题是,如何决定何时展开,何时折叠.

It seems I need to use setExpanded when having IDLE for onScrollStateChanged . question is, how to decide when to expand and when to collapse.

推荐答案

这是一个不错的解决方法,但它仍然与 Play 商店显示的有很大不同:

this is a nice workaround, but It's still very different than what the Play Store shows:

对于每个片段,调用类似的东西:

for each fragment, call something like that:

    recyclerView.addOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            ((MainActivity) getActivity()).onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
            super.onScrolled(recyclerView, dx, dy);
            ((MainActivity) getActivity()).onScrolled(recyclerView, dx,dy);
        }
    });

对于托管活动:

public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
    switch (newState) {
        case RecyclerView.SCROLL_STATE_IDLE:
            mAppBarLayout.setExpanded(mLastDy <= 0, true);
            mLastDy = 0;
            break;
    }
}

public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
    mLastDy = dy == 0 ? mLastDy : dy;
}

不过,如果有人知道如何让它运行良好(使用 ListView 或 RecyclerView 来处理片段),就像在 Play-Store 和其他应用中一样,请写下来.

Still, if anyone knows how to make it work well (using either ListView or RecyclerView for the fragments), just like on the Play-Store and other apps, please write it down.

这篇关于如何使用新的设计库 API 正确隐藏和显示操作栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆