0

我想讓Toolbar摺疊,當用戶滾動TabLayout的標籤之一(由ViewPager提供)。工具欄不能與TabLayout合併

這是我想要的功能:

enter image description here

enter image description here

然而,我的佈局不僅不會滾動,也切斷內容底部(以確切地說,它切斷了48dp - 工具欄的高度):

enter image description here

我使用ViewPager將每個片段顯示爲一個選項卡。碎片包含一個簡單的ScrollView,包含TextView。下面是佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/ueber_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:layout_scrollFlags="scroll|enterAlways" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/ueber_tabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabMode="fixed" 
      app:tabGravity="fill" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:tabIndicatorColor="@android:color/white"/> 

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

    <android.support.v4.view.ViewPager 
     android:id="@+id/ueber_viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

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

每個片段具有如下的佈局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
     android:id="@+id/ueber_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:textColor="#000000" 
     android:linksClickable="false" 
     android:textColorLink="#000000" 
     android:fontFamily="serif" 
     android:padding="16dp"/> 

</ScrollView> 

的ViewPager由ViewPagerAdapter設置:

ViewPager viewPager = (ViewPager) findViewById(R.id.ueber_viewpager); 

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
adapter.addFrag(new ContentFragment(), "Über"); 
adapter.addFrag(new ContentFragment(), "Impressum"); 
adapter.addFrag(new ContentFragment(), "Lizenzen"); 
viewPager.setAdapter(adapter); 

TabLayout tabLayout = (TabLayout) findViewById(R.id.ueber_tabLayout); 
tabLayout.setupWithViewPager(viewPager); 
+0

只需使用從這裏代碼:HTTPS://github.com/chrisbanes/cheesesquare/tree/master/app/src/main –

+0

本博客文章可能有助於http://inthecheesefactory.com/blog/ android-design-support-library-codelab/en – EpicPandaForce

回答

4

使用NestedScrollView代替ScrollView

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
+0

這樣做了!儘管在一個稍微不相關的說明中,我已經在我的應用程序主題中設置了android:windowTranslucentStatus = true,現在當向下滾動時,通過狀態欄可以看到工具欄。儘管如此,我仍然需要該設置,以便可以在狀態欄後方看到抽屜式導航欄。我該如何解決這個問題? – jacobz

-2

當該片段在用戶滾動,隱藏工具欄:

getSupportActionBar().hide(); 

當他們滾動回到頂部,顯示工具欄:

getSupportActionBar().show(); 
+0

不,他正在使用新的設計庫。 –

相關問題