2016-01-24 103 views
17

我有這樣一個佈局如下:CoordinatorLayout與RecyclerView和摺疊頭

enter image description here

(工具欄, 頭視圖,文本視圖,RecyclerView)

我需要的頭被倒塌的時候我滾動recyclerview的項目。 這樣屏幕上就會顯示「選擇項目」和回收站視圖。

我看到了一些當工具欄被摺疊時的例子,但我需要工具欄始終存在。

我應該使用哪種佈局/行爲來完成這項工作?

回答

34

您可以通過具有這種佈局實現它:

<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,這就是它。

注意!的「選擇項」 TextView位置取決於特定的行爲要做到:

  • ,你可以把它作爲你的RecyclerViewAdapter滾動出去了,一旦用戶開始通過RecyclerView滾動的第一元素;
  • 您可以將其添加到AppBarLayout,因此只要您滾動或不滾動,它就會始終粘貼在RecyclerView之上;

你可以在這裏閱讀更多Android Design Support Library這裏Design Support Library (III): Coordinator Layout

我希望,它幫助!

+0

非常感謝!有用!!! – Oleg

+0

工程就像一個魅力!謝謝 – Tooroop

0
The below code is working but not smooth scroll compare reqular recyclerview I thought. 

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