2

我一直在處理與處理ViewPager時CollapsingToolbarLayout工具欄有關的問題。工具欄位置隨分頁改變

問題是工具欄在第二頁(總是僅在第二頁)中的位置不正確。然後在第三,第四......頁面中再次正確。很難解釋徹底,所以我包括圖像。第一張圖片顯示了它應該是什麼樣子的第一頁。第二張圖片顯示了在滑動到第二張時的兩頁 - 注意主頁按鈕。 The first page - which is correct

The second page and the first one together, notice the back button

我曾嘗試與ImageView的更換地圖,我也得到了相同的結果。

活性/片段結構是這樣: ItemActivity - > ItemPagerFragment - > ItemDetailsFragment

工具欄被設置在片段,而不是活性和佈局結構如下:

<android.support.design.widget.CoordinatorLayout 
android:id="@+id/cl_root" 
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:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar_layout" 
    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="wrap_content" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginEnd="64dp" 
     app:expandedTitleMarginStart="48dp" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 


     <com.google.android.gms.maps.MapView 
      android:id="@+id/map_view" 
      android:layout_width="match_parent" 
      android:layout_height="355dp" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax" 
      app:mapType="satellite"/> 

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

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

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

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/swipe_refresh_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="visible" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

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

<include layout="@layout/merge_loading"/> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="@dimen/spacing_small" 
    android:src="@drawable/icon_search_blank" 
    app:layout_anchor="@id/app_bar_layout" 
    app:layout_anchorGravity="bottom|right|end"/> 

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

提前致謝!

更新

阮的Trung HIEU答案的幫助下,我設法想出解決辦法。唯一需要的是從協調器佈局中移除android:fitsSystemWindows =「true」。

+0

昨天也有類似的問題......這設計支持庫您使用的刪除?我通過更新com.android.support:design:22.2.0到22.2.1來解決它 –

+0

您真的需要'CollapsingToolbarLayout'嗎? –

+0

@ incr3diblenoob正在使用23.2.1,並且剛更新到23.3.0,問題依然存在。 – palmithor

回答

0

android:fitsSystemWindows="true" 

android.support.design.widget.CoordinatorLayout 

這是代碼

<android.support.design.widget.CoordinatorLayout 
android:id="@+id/cl_root" 
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:id="@+id/app_bar_layout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
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="wrap_content" 
    app:contentScrim="?attr/colorPrimary" 
    app:expandedTitleMarginEnd="64dp" 
    app:expandedTitleMarginStart="48dp" 
    app:layout_scrollFlags="scroll|exitUntilCollapsed"> 


    <com.google.android.gms.maps.MapView 
     android:id="@+id/map_view" 
     android:layout_width="match_parent" 
     android:layout_height="355dp" 
     app:layout_collapseMode="parallax" 
     app:mapType="satellite"/> 

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

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

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

<android.support.v4.widget.SwipeRefreshLayout 
android:id="@+id/swipe_refresh_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:visibility="visible" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

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

<include layout="@layout/merge_loading"/> 

<android.support.design.widget.FloatingActionButton 
android:id="@+id/fab" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginRight="@dimen/spacing_small" 
android:src="@drawable/icon_search_blank" 
app:layout_anchor="@id/app_bar_layout" 
app:layout_anchorGravity="bottom|right|end"/> 

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

同樣的事情發生,但現在第一個顯示與狀態欄的大小的邊緣,所以它是一種更糟。 – palmithor

+1

嘗試更改android:fitsSystemWindows =「true」at Control不同。 –

+1

您的評論讓我得到了正確的解決方案 - 恰恰相反。它從CoordinatorLayout中移除fitsSystemWindow,但保留在所有其他元素上後,它的工作方式就像一個魅力。 – palmithor