事件分发机制

View的事件分发机制

问题

当给view设置了onTouchListener,onClickListener,onTouchEvent方法,他们的执行顺序是怎么样的?

1. View的dispatchTouchEvent()

@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
          ...
             ListenerInfo li = mListenerInfo;
            if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {
                result = true;
            }

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

如果设置了onTouchListener会先判断onTouch()的返回值。如果为true,onTouchEvent不会执行,为false才会执行

2. View的onTouchEvent()

   public boolean onTouchEvent(MotionEvent event) {
       switch (action) {
                case MotionEvent.ACTION_UP:
                    ...
                       if (!post(mPerformClick)) {
                                    performClick();//执行点击事件
                                }
                    ...
}

在View的onTouchEvent()中会执行onClick(),所以得出结论(假设上述涉及的方法均为默认返回值):
ACTION_DOWN:onTouch() ->onTouchEvent()
ACTION_MOVE:onTouch() ->onTouchEvent()
ACTION_UP:onTouch() ->onTouchEvent()->onClick()

ViewGroup的事件分发机制

当用户点击屏幕时,首先会执行Activity的dispatchTouchEvent()

1.Activity的dispatchTouchEvent()

 public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            onUserInteraction();
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

Activity的dispatchTouchEvent()会调用DecorView的super.dispatchTouchEvent(),接着会执行ViewGroup的dispatchTouchEvent()

2. ViewGroup的dispatchTouchEvent()

   @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
      ...
         //1.拦截判断
            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); 
                } else {
                    intercepted = false;
                }
            } else {
                intercepted = true;
            }
      ...
      //2.判断事件分发
      if (!canceled && !intercepted) {
                if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {//判断事件是否分发给子孩子
                              ...
                                if (preorderedList != null) {
                                    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);//在这里会给mFirstTouchTarget赋值
                                alreadyDispatchedToNewTouchTarget = true;
                                break;
                            }
      }
      ...
      //3.事件分发
            if (mFirstTouchTarget == null) {
                handled = dispatchTransformedTouchEvent(ev, canceled, null,
                        TouchTarget.ALL_POINTER_IDS);
            } else {
            }
      ...

}
  1. 第一个事件序列ACTION_DOWN或者mFirstTouchTarget 不为空时会进入拦截判断
  2. 如果不拦截会进行子孩子的事件分发判断(详见下一步的分析)
  3. 如果mFirstTouchTarget为空,事件序列将不会继续向子孩子进行分发

3.ViewGroup的dispatchTransformedTouchEvent()

  private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
            View child, int desiredPointerIdBits) {
         ...
         if (child == null) {
            handled = super.dispatchTouchEvent(transformedEvent);
        } else {
            final float offsetX = mScrollX - child.mLeft;
            final float offsetY = mScrollY - child.mTop;
            transformedEvent.offsetLocation(offsetX, offsetY);
            if (! child.hasIdentityMatrix()) {
                transformedEvent.transform(child.getInverseMatrix());
            }

            handled = child.dispatchTouchEvent(transformedEvent);
        }
        ...
      return handled;
}

如果child为空,就调用父类的dispatchTouchEvent(),既自己的onTouchEvent;否则调用子类的dispatchTouchEvent(),接着根据上一节View的分发机制判断事件是否被消耗(onTouch,onTouchEvent等等是否返回了true)。所以如果没有子孩子或者子孩子没有消耗事件,handled会返回false,2中的条件不会被执行,从而导致mFirstTouchTarget不被赋值。

你可能感兴趣的:(事件分发机制)