0

I use CoordinatorLayout in conjunction with ViewPager and the ViewPager is taking all the space inside the CoordinatorLayout so the AppBarLayout is completely hidden.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".TabbedScreenActivity"
    android:id="@+id/root"
    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.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

            <android.support.v7.widget.Toolbar
                android:id="@+id/acb_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/primary"
                app:theme="@style/CustomToolbarStyle"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

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

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        </android.support.v4.view.ViewPager>

        <kuwaitnet.cashandcash.gui.views.SlidingTabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/black"
            android:orientation="horizontal">
        </kuwaitnet.cashandcash.gui.views.SlidingTabLayout>

    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Alexandru Circus
  • 5,478
  • 7
  • 52
  • 89

2 Answers2

1

Here is what I currently have in my current project which hides the toolbar when one of the pages in the viewpager is scrolled up. Ignore the drawerlayout and navigationview.

Try adding app:layout_scrollFlags="scroll|enterAlways" to your Toolbar

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

    <android.support.design.widget.CoordinatorLayout 
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


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

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

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />


            <!-- FIX for intermittent invisible app bar layout
            http://stackoverflow.com/questions/30895579/coordinatorlayout-toolbar-invisible-on-enter-until-full-height-->
            <View
                android:id="@+id/appbar_bottom"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@android:color/transparent"
                android:visibility="invisible" />

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

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="15dp"
            android:src="@drawable/ic_add_white_24dp"
            app:backgroundTint="?attr/colorPrimaryDark"
            app:layout_behavior="com.bitbitbitbit.thefreebees.ui.utils.ScrollingFabBehavior" />

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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>
Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
  • No, I just want the toolbar with appName and the action menu buttons. And the toolbar should hide when scrolling vertically any of the ViewPager's page. – Alexandru Circus Aug 27 '15 at 13:33
  • @AlexandruCircus ... Edited. Try adding `app:layout_scrollFlags="scroll|enterAlways"` to your Toolbar. – Mark Pazon Aug 27 '15 at 13:41
  • It works now but the toolbar(when it is visible) covers the viewPager's content. I want to the viewPager to be below the Toolbar and to scroll up when the toolbar hides. I can do this by setting paqddingTop and clipToPadding="false" on my each RecyclerView on each page – Alexandru Circus Aug 27 '15 at 14:19
  • But there is no other way to put the content of the viewPager below the toolbar? – Alexandru Circus Aug 27 '15 at 14:20
1

Source: http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/

Basically, for toolbar to be hiding during scrolling, then you need to specify the flag: app:layout_scrollFlags="scroll|enterAlways"

Thus, your toolbar will be something like this:

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

In short, here's something you can use:

<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.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

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

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

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

</android.support.design.widget.CoordinatorLayout>
Fadils
  • 1,508
  • 16
  • 21