调整 CoordinatorLayout 行为

Adjusting CoordinatorLayout behavior

假设我们有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.SearchView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

在这个布局中,如果我向上拖动SearchView,它会折叠并消失。但我想知道如何将这种拖动效果附加到我的 RecyclerView。意味着我喜欢在向上滚动 RecyclerView 项时折叠 SearchView,但在向下滚动 RecylcerView 项时展开 SearchView。我是否需要为 SearchView 创建自定义行为,或者可以通过可用行为创建此效果?

此致

使用 折叠工具栏布局 并将 searchView 包裹在其中...同时包裹 recyclerViewnestedScrollView 并将 app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 添加到嵌套滚动视图而不是

不要忘记将 scrollFlagscontent_scrim 添加到折叠工具栏布局中..

折叠工具栏布局应在 appBarLayout

作为参考检查一下

https://antonioleiva.com/collapsing-toolbar-layout/

您唯一需要做的更改是将 |enterAlways 添加到您现有的 app:layout_scrollFlags 属性。