Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewpager not appearing when using CollapsingToolbarLayout

I've been playing around with the Design Support Library and have come across a little problem that I hope someone can help me out with.

I'm using a CollapsingToolbar which contain an ImageView which collapses in parralax. In the CollapsingToolbar, I alse have a TabLayout which is supposed to scroll my ViewPager. My issue is that the ViewPager doesn't appear. The issue seems to arise only when I set my ViewPager layout_height to match_parent or wrap_content. If I set it to 600dp, then the problem is gone but then the view ends up being a fixed length which isn't very nice.

Any help would be grealty appreciated!

Here's my 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>
like image 342
Adam Avatar asked Jul 07 '15 17:07

Adam


1 Answers

You should remove the NestedScrollView and LinearLayout levels between your CoordinatorLayout and ViewPager:

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

The view with app:layout_behavior="@string/appbar_scrolling_view_behavior must be a direct child of CoordinatorLayout to be positioned correctly in relation to the AppBarLayout.

like image 69
ianhanniballake Avatar answered Nov 14 '22 23:11

ianhanniballake