2017-03-31 85 views
1

我有圖像的報頭和RecyclerView與一些項滾動頭時滾動recyclerview項

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:id="@+id/header" 
     android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:background="#ffffff"> 


     <ImageView 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:padding="0dp" 
      android:id="@+id/imageView" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" /> 

    </RelativeLayout> 


    <ProgressBar 
     android:id="@+id/progressBar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentStart="true" 
     android:scrollbars="vertical" 
     android:layout_alignParentLeft="true" /> 


</LinearLayout> 

RelativeLayout的固定在RecyclerView的頂部。 我想要的是滾動RecyclerView時滾動此標題(RelativeLayout)。


任何想法如何滾動標題與RecyclerView?

回答

2

使您的根佈局成爲協調器佈局,然後製作協調器佈局的RecyclerView Direct子項。將可滾動內容包裝在AppBarLayout中,然後應用滾動行爲。嘗試下面的代碼,這應該對你有所幫助。

<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_height="wrap_content" 
     android:layout_width="match_parent" 
    > 

    <RelativeLayout 
     android:id="@+id/header" 
     android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="100dp" 
     app:layout_scrollFlags="scroll" 
     android:background="#ffffff"> 


     <ImageView 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:padding="0dp" 
      android:id="@+id/imageView" 
      android:src="@mipmap/ic_launcher" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" /> 

    </RelativeLayout> 



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

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentStart="true" 
    android:scrollbars="vertical" 
    android:layout_alignParentLeft="true" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    /> 



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

非常感謝你,它的工作原理! –