如何在Android中滚动ListView时隐藏ActionBar?

23

2
可能是Hiding the ActionBar on RecyclerView/ListView onScroll的重复问题。 - Kuba Spatny
1
@KubaSpatny 我正在尝试实现,但是你的代码对我不起作用。这不是重复的。之前的帖子也不起作用。 - user4789408
1
@KubaSpatny,您提供的链接是错误的。如果我扩展AppCompatActivity,则无法覆盖onScrollStateChanged和onScroll方法。如果我扩展Activity,则无法使用“getSupportActionBar”。 - user4789408
4个回答

33

如果您想要获得这种行为的列表,您应该:

  • 使用compile 'com.android.support:design:22.2.0'添加设计支持库
  • 使用带有Toolbar的CoordinatorLayout,在其中需要定义app:layout_scrollFlags="scroll|enterAlways"
  • 使用RecyclerView代替ListView。 如此描述:这里 ListViewGridView仅在API> 21时才与CoordinatorLayout具有预期的行为。 在这种情况下,您必须使用setNestedScrollingEnabled(true);

官方博客文章显示了这种情况:

<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

     <! -- Your Scrollable View -->
    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
          <android.support.v7.widget.Toolbar
                  ...
                  app:layout_scrollFlags="scroll|enterAlways">


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

1
我能改变折叠速度吗? - user4789408
1
我认为唯一的方法是使用自定义行为。 - Gabriele Mariotti
还有另一个问题。当工具栏被隐藏并且我离开应用程序、打开另一个应用程序,再次打开该应用程序时,工具栏变成了没有任何内容的矩形,只有一种颜色。 - user4789408
@GabrieleMariotti,使用ListView能否实现相同的行为?使用RecyclerView可以正常工作。但我正在使用ExpandableListView,我需要相同的行为。这是否可能? - Sarath Kn
目前,只有API>21的ListViewGridViewCoordinatorLayout具有预期的行为。请查看此链接:https://dev59.com/MF0a5IYBdhLWcg3wHlik#30885092。 - Gabriele Mariotti
你如何使其与API 18-21兼容? - Shane Sepac

10
我建议使用Google的新支持设计库。 将其包含在您的依赖项中:
compile 'com.android.support:design:22.2.0'

然后使用AppBarLayoutNestedScrollView一起使用。

对于您的Toolbar,定义app:layout_scrollFlags="scroll|enterAlways",这意味着当您滚动时它会消失,并且如果向上滚动则立即返回(这意味着您不必滚动到顶部)。

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways" />

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

@maveň 我其实觉得这个问题还不错。可能是一个重复的问题,但考虑到新库,我想给一个更简单的答案。 - Kuba Spatny
它无法工作,我只能移动操作栏,列表视图却卡住了。 - user4789408
@user4789408 在使用低于21的API时,使用ListView可能会出现问题。相反,请使用RecyclerView - Kuba Spatny

5

使用[CoordinatorLayout]: https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html,它可以允许子视图之间进行协调。就像当另一个视图中观察到某种行为(ListView->滚动)时,在某些视图上执行操作(AppBarLayout->滚动)。

  1. Make Listview nestedScrollingEnabled, works for >API 21

    android:nestedScrollingEnabled="true"
    
  2. Trigger layout behaviour to appbar scrolling.

    android:nestedScrollingEnabled="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    
  3. Any layout(ToolBar/TabLayout/any), which is required to show-hide/scroll, place it inside AppBarLayout, and enabled scroll flag.

    app:layout_scrollFlags="scroll|enterAlways"
    

2
你应该使用CoordinatorLayout来完成这个任务。它是支持设计库的一部分。在这里,在CoordinatorLayout和应用程序栏部分,你可以找到一个例子。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接