【问题标题】:Toolbar doesn't hide on list scroll工具栏不会隐藏在列表滚动中
【发布时间】:2015-12-21 22:43:03
【问题描述】:

所以,我有这个使用CoordinatorLayout 在其下方显示工具栏和选项卡的主要活动布局:

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

    <android.support.design.widget.AppBarLayout
        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"
            android:elevation="0dp"
            local:layout_scrollFlags="scroll|enterAlways"
            local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            local:tabMode="fixed"
            local:tabGravity="fill"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foreground="@drawable/header_shadow"
        local:layout_behavior="@string/appbar_scrolling_view_behavior"  />

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

然后我有 2 个片段布局,每个布局用于我将拥有的 2 个选项卡中的每一个。一个片段在其内容上显示ListView

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@drawable/header_shadow"
    tools:context=".StationsFragment">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <ListView
            android:id="@+id/stations_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

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

</FrameLayout>

当我滚动上面的列表视图时,我希望我的工具栏隐藏起来,我拥有的代码应该没问题,至少从我在answer 中读到的内容来看,但它不起作用。目前我试图从片段活动中删除FrameLayout,但这并没有解决问题。我考虑过对列表视图中的滚动事件进行硬编码,但如果可能的话,我真的希望它能够在没有代码的情况下工作..

提前致谢。

【问题讨论】:

  • 嗯,试试你的xml中的layout_scrollFlags="scroll|enterAlwaysCollapsed"
  • @Coeus 不起作用:/,感谢您的回答

标签: android listview android-tabs


【解决方案1】:

这是因为您将CoordinatorLayoutListView 一起使用。您可以将您的实现更改为RecyclerView 以实现正确的滚动。

检查我的答案here。这可能会对您有所帮助。

【讨论】:

  • 所以除非硬编码滚动事件,否则用ListView 不可能实现这一点,对吧?我正在使用 ListView,因为我从未使用过 RecyclerView,但看来我必须使用它..
  • 确实如此。我们必须转到RecyclerView 以利用与toolbar 一起提供的功能。此滚动效果仅适用于 NestedScrollingChild 的子级
  • 酷:)。快乐编码
【解决方案2】:

尝试改变

local:layout_scrollFlags="scroll|enterAlways"

local:layout_scrollFlags="scroll|exitUntilCollapsed"

【讨论】:

  • 尝试将嵌套滚动添加到您的片段。检查我的编辑
  • 折叠工具栏布局仅适用于嵌套滚动。所以你必须使用回收器视图而不是列表视图。回收站视图具有内置的嵌套滚动....
  • 好的,我明白了.. 既然@sha在你之前说过,我会接受他的回答,但感谢你的帮助,我很感激。
猜你喜欢
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
相关资源
最近更新 更多