使用 CollapsingToolbarLayout 时不显示 Viewpager

Viewpager not appearing when using CollapsingToolbarLayout

我一直在研究设计支持库,遇到了一个小问题,希望有人能帮我解决。

我正在使用一个 CollapsingToolbar,它包含一个在视差中折叠的 ImageView。在 CollapsingToolbar 中,我还有一个应该滚动我的 ViewPager 的 TabLayout。我的问题是 ViewPager 没有出现。只有当我将 ViewPager layout_height 设置为 match_parent 或 wrap_content 时,问题似乎才会出现。如果我将它设置为 600dp,那么问题就消失了,但是视图最终变成了固定长度,这不是很好。

如有任何帮助,我们将不胜感激!

这是我的 XML

<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/main_content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true">

  <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        >

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"
            android:src="@drawable/stock_image"/>

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>

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

  </android.support.design.widget.AppBarLayout>
  <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            />
    </LinearLayout>
  </android.support.v4.widget.NestedScrollView>

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

您应该删除 CoordinatorLayoutViewPager 之间的 NestedScrollViewLinearLayout 级别:

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

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

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

具有 app:layout_behavior="@string/appbar_scrolling_view_behavior 的视图必须是 CoordinatorLayout 的直接子视图才能相对于 AppBarLayout.

正确定位