1、前言


2、正文
2、1、准备工作

<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:title="我的标题" /> <com.google.android.material.tabs.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabTextAppearance="@style/CustomStyle.TabLayoutTextStyle" /> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- Your scrolling content --> <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
2.2、noScroll
Disable scrolling on the view. This flag should not be combined with any of the other scroll flags.
Constant Value: 0 (0x00000000)
禁用视图上的滚动。此标志不应与任何其他滚动标志相结合。
<androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:title="我的标题" />

2.3、scroll
The view will be scroll in direct relation to scroll events.
This flag needs to be set for any of the other flags to take effect.
If any sibling views before this one do not have this flag, then this value has no effect. Constant Value: 1 (0x00000001)
这个文档解释的内容比较多。我们一一来演示。首先在 Toolbar
下添加这个 flag
<androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll" app:title="我的标题" />
Toolbar
可以随着滚动事件而滚动:
现在我们把 app:layout_scrollFlags="scroll" 挪到 TabLayout 上,同时将 Toolbar 上的该属性删除:
<androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:title="我的标题" /> <com.google.android.material.tabs.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll" app:tabTextAppearance="@style/CustomStyle.TabLayoutTextStyle" />
运行效果如下:
可以看到和没有设置任何 layout_scrollFlags 的效果是一模一样的。这是怎么回事呢?

2.4、enterAlways
When entering (scrolling on screen) the view will scroll on any downwards scroll event, regardless of whether the scrolling view is also scrolling.
This is commonly referred to as the 'quick return' pattern. Constant Value: 4 (0x00000004)
当进入(即在屏幕上的滚动手指由上向下滑动)时,设置了这个 flag 的 View一遇到向下的滚动事件就会滚动,不管滚动的 View是否也在滚动中。这通常被称为“快速返回”模式。
<androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" app:title="我的标题" />
运行效果如下:
2.5、enterAlwaysCollapsed
An additional flag for 'enterAlways' which modifies the returning view to only initially scroll back to it's collapsed height. Once the scrolling view has reached the end of it's scroll range, the remainder of this view will be scrolled into view. The collapsed height is defined by the view's minimum height. Constant Value: 8 (0x00000008)
这是 ‘enterAlways’ 的一个附加的 flag。作用是修改返回的 View 的进入效果。首先,返回的 View 会滚动返回到它的折叠高度。当滚动的 View一达到它的滚动范围的末尾时,返回的View还在视图外的部分就会滚动进入视图。折叠高度是通过返回的View的最小高度来定义的。
<androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:minHeight="20dp" app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" app:title="我的标题" />

2.6、exitUntilCollapsed
When exiting (scrolling off screen) the view will be scrolled until it is 'collapsed'. The collapsed height is defined by the view's minimum height. Constant Value: 2 (0x00000002)
<androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:minHeight="20dp" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:title="我的标题" />
运行效果如下:
2.7、snap
Upon a scroll ending, if the view is only partially visible then it will be snapped and scrolled to its closest edge. For example, if the view only has its bottom 25% displayed, it will be scrolled off screen completely. Conversely, if its bottom 75% is visible then it will be scrolled fully into view. Constant Value: 16 (0x00000010)
<androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll|snap" app:title="我的标题" />
运行效果如下:
2.8、snapMargins
An additional flag to be used with 'snap'.
If set, the view will be snapped to its top and bottom margins, as opposed to the edges of the view itself.
Constant Value: 32 (0x00000020)
这是和 ‘snap’ 一起使用的额外的 flag。
如果设置的话,这个 View 将会被 snap 到它的顶部外边距和它的底部外边距的位置,而不是这个 View 自身的上下边缘。
修改 xml,如下:
<androidx.appcompat.widget.Toolbar android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" app:layout_scrollFlags="scroll|snap|snapMargins" app:title="我的标题" />
运行效果如下:
传送门:https://github.com/First-Time/ScrollFlagsDemo (Kotlin)
https://github.com/First-Time/ScrollFlagsJavaDemo (Java)
参考