代码之家  ›  专栏  ›  技术社区  ›  Oleg

带RecyclerView和折叠标题的CoordinatorLayout

  •  47
  • Oleg  · 技术社区  · 9 年前

    我有如下布局:

    enter image description here

    (工具栏, 标题视图、文本视图、回收器视图)

    当我滚动recycleview的项目时,我需要折叠标题。 这样,“选择项目”和“回收”视图就会留在屏幕上。

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

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

    2 回复  |  直到 9 年前
        1
  •  82
  •   janosrusiczki    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: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" 属性集。你使 RecyclerView 通过设置可正确滚动 app:layout_behavior="@string/appbar_scrolling_view_behavior" 差不多就是这样。

    NB! “选择项目”的位置 TextView 取决于您想要实现的特定行为:

    • 您可以将其作为您的 回收站视图 Adapter 一旦用户开始滚动 回收站视图 ;
    • 您可以将其添加到 AppBarLayout 所以它会一直粘在 回收站视图 ,无论您是否滚动;

    你可以在这里阅读更多 Android Design Support Library 还有这里 Design Support Library (III): Coordinator Layout

    我希望,这有帮助!

        2
  •  1
  •   Vadim Kotov First Zero    6 年前

    下面的代码是工作的,但不是平滑的滚动比较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>