我有一个 AppBarLayout,它在它的 child 中包含一个水平的 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实际上是通过 scroll 属性定义 android:layout_scrollFlags 标志。

  • 从那个标志的 official documentation 中,我们可以读到



    这意味着您的标志被忽略,因为其他 child 没有任何标志。我相信这背后的原因是我们只想隐藏顶部 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/

    10-10 04:55