Alexandr
Alexandr

Reputation: 3969

How to collapse content of AppBarLayout but not Toolbar

Here is my application screen:

enter image description here

And layout xml:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:title="@string/app_name"
        app:theme="@style/Toolbar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@drawable/arrow_right_bold"
    app:fabSize="normal" />

And I want to collapse the only content while scrolling.

I figure out that I can move Toolbar out of AppBarLayout but whether there is a clear way? Otherwise, I will be forced set toolbar background color. I haven't any problem with it, but I think that it's some kind of dirty solution :)

So. How to collapse content of AppBarLayout but not Toolbar?

Upvotes: 7

Views: 6137

Answers (3)

Acuna
Acuna

Reputation: 1837

You simply need to wrap your CoordinatorLayout to the LinearLayout and put your toolbar above the CoordinatorLayout:

<LinearLayout
  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:orientation="vertical"
  >
  
  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    />
  
  <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_start"
    >
    
    <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="300dp"
      >
      
      <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >
       
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          app:layout_collapseMode="parallax"
          >
          
          <!-- Collapsed content -->
          
        </LinearLayout>
      
      </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"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      >
      
      <!-- Another content -->
     
    </android.support.v4.widget.NestedScrollView>
    
  </android.support.design.widget.CoordinatorLayout>
  
</LinearLayout>

Upvotes: 0

Black_Prime
Black_Prime

Reputation: 1

Probably I'm too late but if you still need a way to handle this you can follow this article.

The power of AppBarLayout offset

He simply puts the toolbar outside the CoordinatorLayout. Quite radical I know but this is the only way I found.

Upvotes: 0

Logain
Logain

Reputation: 4374

If I understood correctly, you want to collapse a portion of the header when scrolling. If that's the case, @Nerd's comment links to a good tutorial on how to do that. Anyway, I'll leave here an example of CoordinatorLayout usage that does what you are looking for:

<?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:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout

            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <!-- PUT HERE THE PORTION OF THE HEADER YOU WANT TO COLLAPSE WHEN SCROLLING-->
            </FrameLayout>

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


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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- YOUR SCROLLABLE CONTENT HERE -->
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>

Upvotes: 6

Related Questions