【问题标题】:Toolbar doesn't responsive to scroll events工具栏不响应滚动事件
【发布时间】:2017-04-06 10:49:56
【问题描述】:

我想在 ListView 滚动时隐藏/显示工具栏。我有包含工具栏的 CorrdinatorLayout。我将 app:layout_scrollFlags="scroll|enterAlways" 属性添加到工具栏。然后我在 content_min 布局中使用带有 app:layout_behavior="@string/appbar_scrolling_view_behavior" 属性的 RelativeLayout 的 Listview。我在 CoordinatorLayout 中包含了 content_main 布局 - 工具栏不响应列表视图上的滚动事件。此功能是否仅适用于 RecyclerView 而不适用于列表视图?

<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" />

【问题讨论】:

    标签: android


    【解决方案1】:

    滚动行为适用于RecyclerView,但不适用于ListView。这是因为滚动行为需要一个实现NestedScrollingChild接口的滚动组件。此接口由RecyclerView 实现,而不是ListView

    我的建议是将您的ListView 适配器转换为RecyclerView 适配器并使用RecyclerView

    据说有一些方法可以将 ListView 放入 NestedScrollView 中,这将产生滚动行为。我不建议这样做,但如果您搜索 SO,您可以找到一些解决方案,将您的 ListView 包装在 NestedScrollView 中并让工具栏滚动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      相关资源
      最近更新 更多