我们假设我们有以下布局:
<?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
上。意味着每当我向上滚动SearchView
项目时我喜欢折叠RecyclerView
,但是当我向下滚动SearchView
项目时扩展RecylcerView
。我是否需要为SearchView
创建自定义行为,或者可以通过可用行为创建此效果?
最好的祝福
使用折叠工具栏布局并将searchView包装在其中...并使用nestedScrollView包装recyclerView并将app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
添加到嵌套滚动视图中
别忘了将scrollFlags
和content_scrim
添加到折叠工具栏布局中..
折叠工具栏布局应位于appBarLayout
内
如需参考,请查看此内容
您需要做的唯一更改是将|enterAlways
添加到现有的app:layout_scrollFlags
属性中。