我有一个 AppBarLayout
,它的子元素中包含一个水平的 RecyclerView
。
AppBarLayout
的兄弟是 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/