not_again_stackoverflow
not_again_stackoverflow

Reputation: 1323

Make collapsible toolbar automatically collapse/expand

How to make a collapsible toolbar in android automatically collapse/expand when the vertical offset goes beyond a specific threshold?

As in, if the vertical offset is past the half-point of getScrollRange() then the collapsible toolbar should automatically expand, and below that threshold it should collapse.

Upvotes: 3

Views: 4517

Answers (2)

Bronx
Bronx

Reputation: 4510

You can use the snap flag like below:

<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:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

Example Gif

Using this option will determine what to do when a view only has been partially reduced. If scrolling ends and the view size has been reduced to less than 50% of its original, then this view to return to its original size. If the size is greater than 50% of its sized, it will disappear completely.

You can read more about scrolling flags of CollapsingToolbarLayout here:
https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

Upvotes: 12

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21766

Just use 'snap' option to get your desired output. Set Collapsing Toolbar Layout scroll flag as:

app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"

snap: This option will determine what to do when a view only has been partially reduced. If scrolling ends and the view size has been reduced to less than 50% of its original, then this view to return to its original size. If the size is greater than 50% of its sized, it will disappear completely.

I hope this will help.

Upvotes: 2

Related Questions