2016-01-24 102 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>