3

試圖使用新的支持設計庫。在我的佈局中,我希望ToolBar保持在屏幕的頂部,但當用戶向下滾動時,屏幕上會出現TabLayout。但它看起來像ViewPagerAppBarLayout下。我用這個博客帖子參考 https://medium.com/ribot-labs/exploring-the-new-android-design-support-library-b7cda56d2c32新的android支持設計庫,滾動tablayout offscreen不工作

這是佈局,內ViewPager持有Fragments是由回收器看法

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:background="@color/material_grey50" 
android:clickable="true"> 

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tabLayout" /> 


<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_list" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 

     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 


    <android.support.design.widget.TabLayout 
     android:id="@+id/tabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar_list" 
     android:background="?attr/colorPrimary" 
     android:scrollbars="horizontal" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:tabGravity="center" /> 


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


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab_post" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_gravity="bottom|right" 
    android:layout_marginBottom="16dp" 
    android:layout_marginRight="16dp" 
    android:elevation="3dp" 
    android:src="@drawable/ic_videocam_white_36dp" 
    app:backgroundTint="@color/accent" 
    app:fabSize="normal" /> 

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

這裏的layout_behavior與回收站視圖或嵌套滾動視圖等特定控件一起使用。看[demo](https://github.com/chrisbanes/cheesesquare)。 – Harry

回答

0

不要忘記設置這個在viewpager:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

+0

我這樣做也沒有效果 – Brian

+0

這可能不是這樣,但當ViewPager是最後一個孩子,而AppBarLayout是CoordinatorLayout的第一個孩子時,它可以在我的項目中正常工作。嘗試按順序重新排序。另外,我看到''標籤添加了代碼片段的底部,所以如果這是以前刪除的剩餘部分,請嘗試刪除它。 –

+0

嗯重新排序不起作用,你使用設計支持22.2.0和支持庫22.2?這個佈局是否在一個片段中會有影響嗎? – Brian

相關問題