not_again_stackoverflow not_again_stackoverflow -4 years ago 140
Android Question

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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download