[英]Toolbar doesn't responsive to scroll events
我想在ListView滾動時隱藏/顯示工具欄。 我具有包含工具欄的CorrdinatorLayout。我將app:layout_scrollFlags =“ scroll | enterAlways”屬性添加到工具欄。 然后,我在具有RelativeLayout的content_min布局中使用具有app:layout_behavior =“ @ string / appbar_scrolling_view_behavior”屬性的Listview。 我在CoordinatorLayout -Toolbar中包含了content_main布局,該布局對列表視圖上的滾動事件沒有響應。 此功能僅適用於RecyclerView而不適用於Listview嗎?
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.era.www.onmovie.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
content_main布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.era.www.onmovie.MainActivity"
tools:showIn="@layout/activity_main">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
滾動行為適用於RecyclerView
但不適用於ListView
。 這是因為滾動行為需要實現NestedScrollingChild
接口的滾動組件。 此接口由RecyclerView
實現,而不由ListView
。
我的建議是將ListView
適配器轉換為RecyclerView
適配器並使用RecyclerView
。
大概有一些方法可以將ListView
放在NestedScrollView
,這將提供滾動行為。 我不建議這樣做,但是如果您進行搜索,則可以找到一些將ListView
包裹在NestedScrollView
並使工具欄滾動的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.