2016-11-22 48 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之内,并让工具栏滚动。