2017-04-04 77 views
0

我在它後面有一個標題和一個工具欄。 向上滾動標題時,工具欄將固定在頂部。修復下面的工具欄頂部和標題

問題是,我想要標題前的工具欄,在我的活動之上,並且當我向上滾動標題時,工具欄只會改變它的顏色。

toolbar 
header (when scroll, change toolbar color) 

任何想法如何添加工具欄之前的標題,我的活動之上,並在滾動標題時更改其顏色?

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    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:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <RelativeLayout android:id="@+id/header" 
     android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="160dp" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll"> 


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

     </RelativeLayout> 


      <android.support.v7.widget.Toolbar 
       android:id="@+id/anim_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="?attr/colorPrimaryDark" /> 


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

    </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="none" 
     android:layout_alignParentLeft="true" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

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

您的佈局似乎**嵌套佈局的混亂* *。你知道**嵌套佈局會影響表演**,對嗎?你不能**儘可能簡化它嗎? –

+0

謝謝你的回答。 @Rotwang。我怎麼能簡化這個?我需要有一個標題和一個recyclerview並滾動到一起,我認爲這是唯一的方法... –

+1

像往常一樣,有1000多種方法可以做同樣的事情。我正在考慮使用工具欄/操作欄的容器(比如LinearLayout或者最適合的佈局)。在它裏面,你可以把一個帶View的RecyclerView作爲頭部使用。 「標題」將與RecyclerView中的其他元素一起滾動。 –

回答

1

試試這個:

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <RelativeLayout android:id="@+id/header" 
       android:layout_width="match_parent" 
       android:layout_height="220dp" 
       android:background="@color/colorAccent" 
       app:layout_collapseMode="parallax"> 

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

      </RelativeLayout> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/anim_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="?attr/colorPrimaryDark" /> 
     </android.support.design.widget.CollapsingToolbarLayout> 

    </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="none" 
     android:layout_alignParentLeft="true" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
     --> 
    <include layout="@layout/content_scrolling"></include> 
</android.support.design.widget.CoordinatorLayout> 

輸出

enter image description here

希望這將有助於〜

+0

非常感謝,我會試試! –