CoordinatorLayout 与 RecyclerView 和折叠 header

CoordinatorLayout with RecyclerView And Collapsing header

我的布局如下:

(工具栏, Header视图、文本视图、RecyclerView)

我需要在滚动 recyclerview 的项目时折叠 header。 使视图"Choose item"和recyclerview留在屏幕上。

我看到了折叠工具栏时的示例,但我需要工具栏始终存在。

我应该使用哪个 layouts/behavior 来完成这项工作?

你可以通过这样的布局来实现:

<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>

您可以通过设置 app:layout_collapseMode="pin" 属性 来固定工具栏。您通过设置 app:layout_behavior="@string/appbar_scrolling_view_behavior" 使 RecyclerView 可以正确滚动,仅此而已。

注意! "Choose item" TextView 的位置取决于您想要实现的特定行为:

  • 您可以将其作为 RecyclerViewAdapter 的第一个元素包含在内,以便在用户开始滚动浏览 RecyclerView;
  • 时将其滚动
  • 您可以将它添加到 AppBarLayout 中,这样无论您是否滚动它,它都会始终粘在 RecyclerView 的顶部;

您可以在此处阅读更多内容 Android Design Support Library and here Design Support Library (III): Coordinator Layout

希望对您有所帮助!

下面的代码有效,但滚动不流畅,比较我认为的 reqular recyclerview。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <com.sliderbanner.views.BannerSlider
                android:id="@+id/banner_slider1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:animateIndicators="true"
                app:defaultIndicators="dash"
                app:interval="5000"
                app:loopSlides="true"

                />

            <android.support.v7.widget.Toolbar

                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize">

                <ImageView
                    android:id="@+id/image_github"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:layout_gravity="right"
                    android:layout_marginRight="8dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fontFamily="sans-serif-bold"
                    android:gravity="center_vertical|left"
                    android:text="Banner Slider"
                    android:textColor="@android:color/black"
                    android:textSize="18sp" />
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>


    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>


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