接着上期结尾所述 我们引入一些问题吧。
为什么图片轮播器里的图片使用button不用ImageView?
因为button是可点击的,在onTouchEvent方法中可以进入判断体中返回true。而ImageView是不可点击的。所以需要修改的话,我们可以给ImageView增加android:clickable="true"的属性。这样遍得到了解决。还有其他改法,这边就不说了。
关于拦截
1、如何拦截
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
//如果你觉得需要拦截
return true ;
case MotionEvent.ACTION_MOVE:
//如果你觉得需要拦截
return true ;
case MotionEvent.ACTION_UP:
//如果你觉得需要拦截
return true ;
}
return false;
}
我们只需要重写onInterceptTouchEvent方法就好啦。默认是不拦截的,也就是return false;拦截就return true;这样就不会往下传递了。
2、如何不被拦截
当被拦截,我们子View依然想要相应MOVE及后续的事件要怎么办呢?我们只需要在子View中的dispatchTouchEvent中这么写:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
getParent().requestDisallowInterceptTouchEvent(true);
.....
return super.dispatchTouchEvent(event);
}
关键代码getParent().requestDisallowInterceptTouchEvent(true);
事件分发机制:
Activity事件分发:
一般情况下事件列都是用户按下(DOWN)的那一刻产生的,所以几个方法很重要dispatchTouchEvent()、onTouchEvent()、onInterceptTouchEvent()
dispatchTouchEvent是负责事件分发的,事件首先会传递给activity,我们来看看activity的该方法:
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
事件一开始,一般都是ACTION_DOWN,所以一般都会调用到 onUserInteraction(),我们看源码:
public void onUserInteraction() {
}
这个方法里面没有东西,我们根据注释了解到,此方法主要的作用实现屏保,并且当Activity在栈顶时,点击Home,Back等键都会触发此方法。
接下来就是第二个if中的getWindow().superDispatchTouchEvent(ev)方法了,获取window,window是一个抽象类,所以我们能拿到他的子类PhoneWindow。来看看:
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
return mDecor.superDispatchTouchEvent(event)
}
mDecor是DecorView,DecorView继承FrameLayout,FrameLayout作为ViewGroup的子类,就直接调用了ViewGroup.dispatchTouchEvent();
ViewGroup事件分发:
直接查看ViewGroup的事件分发方法吧,这边不完整发出来源码,一段一段发:
// Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
// There are no touch targets and this action is not an initial down
// so this view group continues to intercept touches.
intercepted = true;
}
其中intercept表示是否要拦截事件,它被onInterceptTouchEvent(ev);的返回值赋值,我们可以通过重写onInterceptTouchEvent(ev)方法来改变返回值。默认情况下是返回false。
我们接着看其他主要的判断
boolean alreadyDispatchedToNewTouchTarget = false;
if (!canceled && !intercepted) {
.......
我们忽略判断canceled,大多数时候都返回false,我们没重写onInterceptTouchEvent并返回true时,都是能进入到这个if中的,其中这个判断中最重要的是这个for循环,来看:
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
// If there is a view that has accessibility focus we want it
// to get the event first and if not handled we will perform a
// normal dispatch. We may do a double iteration but this is
// safer given the timeframe.
if (childWithAccessibilityFocus != null) {
if (childWithAccessibilityFocus != child) {
continue;
}
childWithAccessibilityFocus = null;
i = childrenCount - 1;
}
if (!child.canReceivePointerEvents()|| !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
newTouchTarget = getTouchTarget(child);
if (newTouchTarget != null) {
// Child is already receiving touch within its bounds.
// Give it the new pointer in addition to the ones it is handling.
newTouchTarget.pointerIdBits |= idBitsToAssign;
break;
}
resetCancelNextUpFlag(child);
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
// Child wants to receive touch within its bounds.
mLastTouchDownTime = ev.getDownTime();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
// The accessibility focus didn't handle the event, so clear
// the flag and do a normal dispatch to all children.
ev.setTargetAccessibilityFocus(false);
}
它通过倒序遍历ViewGroup下面所有的子View,然后一个一个判断点击位置是否是该子View的布局区域,还有其他的处理,我这边就不细说了。
如果说是点击在子View的区域的话,就会进入子View的dispatchTouchEvent中。ViewGroup说到底是一个View,我们就必须说说View的事件分发。
View事件分发:
View的事件分发我们上期说过了,就不赘述了。但我们来重新捋一遍。
dispatchTouchEvent()
----onTouch()
----onTouchEvent()
----performClickInternal()
----performClick()
----onClick()
dispatchTouchEvent进行事件分发,其中有一个if中有4个与判断,其中onTouch是第四个条件。其他条件我们忽略,当onTouch返回false时,才会进入到onTouchEvent方法中。这也是onTouch优先级比onTouchEvent高的原因。如果是可点击的(clickable)或者是可被长按点击(long_clickable)就会进入它的(onTouchEvent)判断在ACTION_UP时体中,最后会返回true,会调用到performClickInternal()方法。performClickInternal中调用performClick事件,它就会调用我们设置的点击事件onClick()方法
有什么错误希望得到大家的指正。