工具栏不会隐藏在列表滚动中
Toolbar doesn't hide on list scroll
所以,我有这个主要的 activity 布局,使用 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 中读到的内容来看是这样,但它不起作用。目前我试图从片段 activity 中删除 FrameLayout
但这并没有解决问题。我已经考虑过对列表视图中的滚动事件进行硬编码,但如果可能的话,我真的希望它能在没有代码的情况下工作。
提前致谢。
尝试更改
local:layout_scrollFlags="scroll|enterAlways"
至
local:layout_scrollFlags="scroll|exitUntilCollapsed"
这是因为您正在使用 CoordinatorLayout
和 ListView
。您可以将实现更改为 RecyclerView
以实现正确的滚动。
检查我的回答 。这可能对你有帮助。
所以,我有这个主要的 activity 布局,使用 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 中读到的内容来看是这样,但它不起作用。目前我试图从片段 activity 中删除 FrameLayout
但这并没有解决问题。我已经考虑过对列表视图中的滚动事件进行硬编码,但如果可能的话,我真的希望它能在没有代码的情况下工作。
提前致谢。
尝试更改
local:layout_scrollFlags="scroll|enterAlways"
至
local:layout_scrollFlags="scroll|exitUntilCollapsed"
这是因为您正在使用 CoordinatorLayout
和 ListView
。您可以将实现更改为 RecyclerView
以实现正确的滚动。
检查我的回答