工具栏不会与TabLayout折叠

时间:2015-08-08 21:02:31

标签: android android-toolbar androiddesignsupport android-tablayout

当用户在其中一个Toolbar标签页(由TabLayout提供)中滚动时,我想让ViewPager崩溃。

这是我想要的功能:

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);

2 个答案:

答案 0 :(得分: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">

答案 1 :(得分:-2)

当用户滚动到片段时,隐藏工具栏:

getSupportActionBar().hide();

当它们滚动回到顶部时,显示工具栏:

getSupportActionBar().show();
相关问题