CoordinatorLayout与RecyclerView和Collapsing标头

Ole*_*leg 28 android android-layout material-design androiddesignsupport android-coordinatorlayout

我有如下布局:

在此输入图像描述

(工具栏,标题视图,文本视图,RecyclerView)

当我滚动recyclerview的项目时,我需要折叠标题.这样就可以在屏幕上看到"选择项目"和recyclelerview视图.

我看到工具栏正在折叠时的示例,但我总是需要工具栏.

我应该使用哪些布局/行为来完成这项工作?

Kon*_*nov 55

你可以通过这个布局来实现它:

<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>
Run Code Online (Sandbox Code Playgroud)

您可以通过app:layout_collapseMode="pin"设置属性来固定工具栏.您RecyclerView通过设置可以正确滚动app:layout_behavior="@string/appbar_scrolling_view_behavior",这就是它.

NB!"选择项目"的位置TextView取决于您希望实现的特定行为:

  • 一旦用户开始滚动浏览,你就可以把它作为你RecyclerView的第一个元素Adapter来滚动它RecyclerView.
  • 你可以添加它,AppBarLayout所以它总是会粘在它上面RecyclerView,无论你滚动与否;

您可以在此处阅读更多Android设计支持库和此处的设计支持库(III):协调器布局

我希望,这有帮助!