CoordinatorLayout与RecyclerView和折叠头
我有类似下面的布局:
(工具栏,
标题视图,文本视图,RecyclerView)
(Toolbar, Header View, Text View, RecyclerView)
我需要折叠头,当我滚动recyclerview的项目。
这样的观点选择项,并留下recyclerview在屏幕上。
I need the header to be collapsed when I scrolling recyclerview's items. So that the view "Choose item" and recyclerview left on the screen.
我看到的例子工具栏被倒塌的时候,但我需要的工具栏是present始终。
I saw examples when toolbar is being collapsed, but I need toolbar to be present always.
我应该使用哪些布局/行为得到这个工作的?
Which layouts/behavior should I use to get this work?
您可以通过让此布局存档:
You can archive it by having this layout:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!-- HEADER -->
<RelativeLayout
...
app:layout_collapseMode="parallax">
.....
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
<!-- IF YOU WANT TO KEEP "Choose Item" always on top of the RecyclerView, put this TextView here
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="choose item" />
-->
</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.CoordinatorLayout>
您通过让应用针工具栏:layout_collapseMode =针
属性集。你让 RecyclerView
正常滚动通过设置应用:layout_behavior =@字符串/ appbar_scrolling_view_behavior
,这就是pretty多吧。
You pin your toolbar by having app:layout_collapseMode="pin"
property set. You make RecyclerView
properly scrollable by setting app:layout_behavior="@string/appbar_scrolling_view_behavior"
and that's pretty much it.
NB 的选择项位置的TextView
依赖于特定的行为你要存档:
NB! Position of "Choose item" TextView
depends on the particular behaviour your want to archive:
- 您可以把它作为第一要素你的
RecyclerView
适配器
滚动它扔掉,一旦用户开始滚动通过RecyclerView
; - 您可以将其添加到
AppBarLayout
所以它会一直坚持对的RecyclerView
,当你滚动顶部与否;
- you can include it as a first element of your
RecyclerView
'sAdapter
to scroll it away, once user start scrolling through theRecyclerView
; - you can add it into
AppBarLayout
so it'd always stick on top of theRecyclerView
, whenever you scroll it or not;
您可以在这里阅读更多 Android设计支持库这里设计支持库(III):协调布局
You can read more here Android Design Support Library and here Design Support Library (III): Coordinator Layout
我希望它能帮助!