2016-11-10 208 views
1

當我實現了摺疊式工具在我的活動之一,並在滾動工具欄不垮recyclerview不垮:摺疊工具欄和滾動

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/swipeContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="@color/white" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:fitsSystemWindows="true" 
     android:layout_height="@dimen/app_bar_height" 
     android:layout_width="match_parent" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

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

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

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/my_recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:scrollbars="none" /> 


</LinearLayout> 
</android.support.v4.widget.SwipeRefreshLayout> 

我還添加了所需的庫搖籃。向下滾動時,工具欄保持在同一位置。任何想法爲什麼發生這種情況?

回答

1

AppBarLayout需要CoordinatorLayout。

該視圖在很大程度上依賴於在CoordinatorLayout中用作直接子對象。如果你在不同的ViewGroup中使用AppBarLayout,它的大部分功能將不起作用。

從developer.android.com/reference/android/support/design/widget/AppBarLayout.html

引用

而且看看這些教程: http://antonioleiva.com/collapsing-toolbar-layout/

+0

我有recyclerview –

+0

@RainMan在另一個佈局:你不需要在這個LinearLayout中。 CoordinatorLayout將根據他們的行爲相應地定位視圖。刪除'LinearLayout',將'SwipeRefreshLayout'移動到'RecyclerView'周圍,然後將頂層設置爲'CoordinatorLayout'。 – DeeV

+0

@DeeV,感謝您的評論,我做了與您所描述的相同的方法,但是現在當recycleview的內容位於工具欄的頂部時。 –

1

此代碼可以幫助你。在您的活動中定義CollapsingToolbar並寫入 展開和摺疊的方法。

<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_car_tracker" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <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" 
     tools:context="com.example.user.Activity" /> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="180dp" 
     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" 
      android:background="@color/toolbarbackgroundcolor" 
      android:fitsSystemWindows="true" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="@color/toolbarbackgroundcolor" 
       android:minHeight="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_gravity="center" 
        android:foregroundGravity="center" 
        android:src="@drawable/location" 
        android:scaleType="centerCrop" 
        app:layout_collapseMode="parallax"/> 

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



</android.support.design.widget.CoordinatorLayout> 
相關問題