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