2

I implemented the collapsing toolbar in one of my activities and upon scrolling the toolbar does not collapse:

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipeContainer" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/white" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:fitsSystemWindows="true" android:layout_height="@dimen/app_bar_height" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:contentScrim="?attr/colorPrimary"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:scrollbars="none" /> </LinearLayout> </android.support.v4.widget.SwipeRefreshLayout> 

I also added the required libraries to gradle. The toolbar stays at the same spot when scrolling down. Any idea why this is happening?

2 Answers 2

1

The AppBarLayout needs the CoordinatorLayout.

This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different ViewGroup, most of it's functionality will not work.

Quoted from developer.android.com/reference/android/support/design/widget/AppBarLayout.html

Also have a look at these tutorials : http://antonioleiva.com/collapsing-toolbar-layout/

Sign up to request clarification or add additional context in comments.

4 Comments

I have another layout inside that recyclerview
@RainMan: You don't need a LinearLayout in this. CoordinatorLayout will position the Views accordingly based on their behaviors. Remove the LinearLayout, move the SwipeRefreshLayout around the RecyclerView, then make the top level a CoordinatorLayout.
@DeeV, thanks for comment, I did the same method as you described, but now when the content of recycleview is on top of the toolbar.
@RainMan: Could you add the layout_behavior to the recycler view. app:layout_behavior="@string/appbar_scrolling_view_behavior" and this should solve the issue.
1

This code may help you.Define CollapsingToolbar in your Activity and write the method for expand and collapse.

<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" android:id="@+id/activity_car_tracker" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.user.Activity" /> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="180dp" 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:background="@color/toolbarbackgroundcolor" android:fitsSystemWindows="true" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/toolbarbackgroundcolor" android:minHeight="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:foregroundGravity="center" android:src="@drawable/location" android:scaleType="centerCrop" app:layout_collapseMode="parallax"/> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.