1.事件分發(fā)介紹
2.Down、up事件的分發(fā)過程
3.onTouchListener、onClickListener調(diào)用時機(jī)
4.事件攔截應(yīng)用
5.NestedScrollingParent
6.Behavior的使用
7.NestedScrollingChild接口來源
講完上面的NestedScrollingParent接口后,你會發(fā)現(xiàn)在Vew、ViewGroup也有相似的身影。


由此可以看出二者的方法時基本一致的,NestedScrollingChild接口只有方法,而View的接口里還有方法體。并且和NestedScrollingChildHelper中的方法基本一致,以下是二者dispatchNestedScroll()方法。


我們可以看出,二者基本是相似的。因此我推測最早開始這些方法都是在View中寫的,但是后來把這些方法抽象成了接口,并將方法提取成Helper類。
因此查看了下源碼,ViewGroup的onNestedScroll方法是在api21增加的,NestedScrollingParent、NestedScrollingParentHelper接口是在api22.1新增,而NestedScrollingParent2則是在api26.1新增。
ViewGroup中的方法與NestedScrollingParent中的接口一致。
但是又一處是不太一樣的,在NestedScrollingChildhelper中調(diào)用onNestedPreScroll()方法時使用的是ViewParentCompat.onNestedScroll(),而在View中調(diào)用 mNestedScrollingParent.onNestedScroll(),即ViewGroup中的方法。
public static void onNestedScroll(ViewParent parent, View target, int dxConsumed,
int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
if (parent instanceof NestedScrollingParent2) {
// First try the NestedScrollingParent2 API
((NestedScrollingParent2) parent).onNestedScroll(target, dxConsumed, dyConsumed,
dxUnconsumed, dyUnconsumed, type);
} else if (type == ViewCompat.TYPE_TOUCH) {
// Else if the type is the default (touch), try the NestedScrollingParent API
IMPL.onNestedScroll(parent, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
}
}
若父view實(shí)現(xiàn)的NestedScrollingParent2接口,則調(diào)用之,其他則調(diào)用父View實(shí)現(xiàn)的其他onNestedScroll接口。其中NestedScrollingParent2接口較NestedScrollingParent接口增加了一個參數(shù)NestedScrollType。
boolean onStartNestedScroll(@NonNull View child, @NonNull View target, @ScrollAxis int axes,
@NestedScrollType int type);
改參數(shù)取值為:TYPE_TOUCH、TYPE_NON_TOUCH,是為了解決快速滑動后Fling的傳遞問題,使得滑動Fling可以在父View、子View之間傳遞。TYPE_TOUCH指用戶手指在屏幕上滑動,TYPE_NON_TOUCH為用戶手指離開屏幕后的滑動,即Fling。
綜上,如果你不涉及Fling,則使用View、ViewGroup里面的函數(shù)也可實(shí)現(xiàn)滑動交互,但還是推薦實(shí)現(xiàn)接口的方法,更加清楚簡潔。
現(xiàn)在新的RecyclerView、NestedScrollView都是使用的接口的形式實(shí)現(xiàn),而比較老的ListView、ScrollView則是使用的ViewGroup中的方法。