2016-11-22 50 views
0

我想在ListView滾動時隱藏/顯示工具欄。我有包含工具欄的CorrdinatorLayout。我添加應用程序:layout_scrollFlags =「scroll | enterAlways」屬性到工具欄。然後,我在Content_min佈局中使用RelativeLayout和app:layout_behavior =「@ string/appbar_scrolling_view_behavior」屬性創建了ListView。我在CoordinatorLayout中包含content_main佈局 - 工具欄對listview上的滾動事件沒有響應。此功能是否僅適用於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" /> 

回答

0

滾動行爲可與RecyclerView但不亂盒ListView。這是因爲滾動行爲需要實現NestedScrollingChild接口的滾動組件。此接口由RecyclerView實施,但不是ListView

我的建議是將您的ListView適配器轉換爲RecyclerView適配器並使用RecyclerView

假設有一些方法可以將ListView置於NestedScrollView之內,這將給滾動行爲。我不建議這樣做,但是如果你搜索的話,你可以找到一些解決方案,將ListView包裹在NestedScrollView之內,並讓工具欄滾動。