如何正确地隐藏和放大器;使用新的设计库API显示的动作条? [英] How to properly hide&show the actionbar using the new design library API?

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

问题描述

假设我已经在活动中的这些元素:

Suppose I have these elements in the activity:

  • 在动作条(实际上是一个工具栏)
  • 标签(使用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) :

我已经找到了<一href="https://github.com/ksoichiro/Android-ObservableScrollView">Android-ObservableScrollView"库,它展示了如何做到这一点对很多类型的滚动的看法,但它不处理新TabLayout使用的情况下,(再加上它有问题),如在新的样本,像的 cheesesquare

I've found the "Android-ObservableScrollView" library which shows how to do it for many types of scrolling views, but it doesn't handle the case of the new TabLayout usage (plus it has issues), as shown on newer samples, like cheesesquare.

在设计库(在cheesesquare例所示)的新的API,它更自动,所以你并不需要创建这么多的样板用于处理滚动和如何处理它。

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>

这hirerchy承诺,小吃店将低于晶圆厂,而且操作栏会自动动画(显示/隐藏)根据碎片的内部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.

什么才能让列表视图为上述机构的工作呢?

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

有没有办法做到这一点?如果是这样,怎么样?

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

编辑:不幸的是,似乎谷歌不会支持ListView的这种行为,但仅限于RecyclerView和NestedScrollView(写的 此处 )。

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

所以我试图用<一href="https://github.com/ksoichiro/Android-ObservableScrollView">Android-ObservableScrollView"图书馆,但它有一个很大的滚动问题,包括一扔问题。

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

现在我已经决定给<一个href="https://github.com/kmshack/Android-ParallaxHeaderViewPager">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的片段),就像在播放,存储和其他应用程序,请把它写下来。

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天全站免登陆