美文网首页
事件分发机制解析(二)

事件分发机制解析(二)

作者: caichenor | 来源:发表于2020-01-06 16:12 被阅读0次

(二)dispatchTouchEvent方法

 @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

(三)onTouchEvent方法

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

此处如果返回true时候,再dispatchTouchEvent方法中

if (!result && onTouchEvent(event)) {
                result = true;
}
return result ;

dispatchTouchEvent就返回true。

 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
            View child, int desiredPointerIdBits) {
        final boolean handled;

        // Canceling motions is a special case.  We don't need to perform any transformations
        // or filtering.  The important part is the action, not the contents.
        final int oldAction = event.getAction();
        if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
            event.setAction(MotionEvent.ACTION_CANCEL);
            if (child == null) {
                handled = super.dispatchTouchEvent(event);
            } else {
//此时返回true
                handled = child.dispatchTouchEvent(event);
            }
            event.setAction(oldAction);
            return handled;
        }

此时返回ViewGroup方法中,返回Handled。

//此处进入dispatchTransformedTouchEvent方法中,内部调用addTouchTarget,给touch做上标记
  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;
                            }

此处break直接退出循环,其他子View不会获得event事件。

  private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
        final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
        target.next = mFirstTouchTarget;
        mFirstTouchTarget = target;
        return target;
    }

此处进入addTouchTarget方法,进行标记。mFirstTouchTarget的传给touch的下一个引用。

相关文章

网友评论

      本文标题:事件分发机制解析(二)

      本文链接:https://www.haomeiwen.com/subject/psskactx.html