1.事件分發(fā)介紹
2.Down芯砸、up事件的分發(fā)過程
3.onTouchListener态秧、onClickListener調(diào)用時機
4.事件攔截應用
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實現(xiàn)的NestedScrollingParent2接口革半,則調(diào)用之,其他則調(diào)用父View實現(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ù)也可實現(xiàn)滑動交互垒酬,但還是推薦實現(xiàn)接口的方法,更加清楚簡潔件炉。
現(xiàn)在新的RecyclerView勘究、NestedScrollView都是使用的接口的形式實現(xiàn),而比較老的ListView妻率、ScrollView則是使用的ViewGroup中的方法乱顾。