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)
參考