自动制作可折叠工具栏 collapse/expand

Make collapsible toolbar automatically collapse/expand

当垂直偏移超过特定阈值时,如何在android 中自动collapse/expand 制作可折叠工具栏?

例如,如果垂直偏移超过 getScrollRange() 的半点,则可折叠工具栏应自动展开,低于该阈值时应折叠。

您可以像下面这样使用 snap 标志:

<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.

您可以在此处阅读有关 CollapsingToolbarLayout 的滚动标志的更多信息:
https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

只需使用 'snap' 选项即可获得所需的输出。将 折叠工具栏布局 滚动标志设置为:

app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"

snap: 此选项将决定当视图仅被部分缩小时要执行的操作。如果滚动结束并且视图大小已减小到其原始大小的 50% 以下,则此视图将 return 变为其原始大小。如果尺寸大于其尺寸的 50%,它将完全消失。

希望对您有所帮助。