android - 滚动时 AppBarLayout 子项不会折叠

标签 android android-support-library android-design-library android-appbarlayout

我有一个 AppBarLayout,它的子元素中包含一个水平的 RecyclerViewAppBarLayout 的兄弟是 ViewPager(其中包含一个垂直的 RecyclerView)。

这是 XML:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/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"
    tools:context=".ui.search.SearchResultFragment">

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:overScrollMode="never"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="@dimen/d2"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <LinearLayout
        android:id="@+id/header_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@color/white"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/back_image_view"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:scaleType="center"
            android:src="@drawable/ic_backarrow_gray"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/query_terms_recycler_view"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"/>

        <ImageView
            android:id="@+id/search_image_view"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:scaleType="center"
            android:src="@drawable/ic_search_gray"/>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/related_terms_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/search_term_bg"
        app:layout_scrollFlags="scroll|enterAlways"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/d8"
        android:background="@color/tab_and_nav_bar"/>

</android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

我已经有了另一个带有 AppBarLayout 的 UI,它能够使用 Toolbar 成功响应滚动。代码几乎相同,但在这种情况下出现了问题,因为 RecyclerView 没有折叠/隐藏。

最佳答案

  • AppBarLayout 只是一个垂直的 LinearLayout,所以顺序很重要;
  • 让 child 对滚动手势使用react的实际上是通过 android:layout_scrollFlags 属性定义 scroll 标志。

来自official documentation对于那个标志,我们可以读到

The view will be scroll in direct relation to scroll events. This flag needs to be set for any of the other flags to take effect. If any sibling views before this one do not have this flag, then this value has no effect.

这意味着您的标志被忽略,因为其他 child 没有任何标志。我相信这背后的原因是我们只想隐藏 top View - 如果上面还有东西,底部 View 就不会消失。

所以在这种情况下(您可能不喜欢)的解决方案是:

  • 将该 View 移到顶部,这样它将是唯一退出的 View ;
  • 为所有 View 提供 scroll 标志。它们会一起对滚动手势使用react,这可能不是您想要的。

    <AppBarLayout>
        <RecyclerView ...
            android:layout_scrollFlags="scroll|enterAlways"/>
    
        <LinearLayout ... >
        <TabLayout ... >
    </AppBarLayout>
    

关于android - 滚动时 AppBarLayout 子项不会折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31112339/

相关文章:

android - 设置 CollapsingToolbarLayout 的起始高度

android - 在 Eclipse 中使用支持设计库

android - 即使在提供 "app:layout_scrollFlags"标志后,工具栏也不会滚动

android - 无法识别 OBB 文件

android - 如何使用工具栏在底部显示操作项

android - Android 手机源代码/驱动程序的开放性

android - 我应该如何在android studio中导入Toolbar工具?

android - 依赖项是最新的,但 Gradle 认为不是

Android:如何获取 Gson LinkedTreeMap 值?

java - 为什么运行方法结束时的打印输出语句不打印出来