View系列-onClick执行流程

1.设置监听器

//frameworks\base\core\java\android\view\View.java
public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {
    
    /**
     * Register a callback to be invoked when this view is clicked. If this view is not
     * clickable, it becomes clickable.
     *
     * @param l The callback that will run
     *
     * @see #setClickable(boolean)
     * 如果一开始这个view无法点击的话,会设置成可点击
     */
    public void setOnClickListener(@Nullable OnClickListener l) {
        if (!isClickable()) {
            setClickable(true);
        }
        getListenerInfo().mOnClickListener = l;
    }
}

2.View.dispatchTouchEvent

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {

    public boolean dispatchTouchEvent(MotionEvent event) {
        
        //省略代码

        if (onFilterTouchEventForSecurity(event)) {
            //noinspection SimplifiableIfStatement
            ListenerInfo li = mListenerInfo;
            if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {//执行mOnTouchListener
                //如果执行mOnTouchListener.onTouch返回true,就不会再执行onTouchEvent
                result = true;
            }

            if (!result && onTouchEvent(event)) {//执行onTouchEvent
                result = true;
            }
        }

        //省略代码

    }
}

3.View.onTouchEvent 

MotionEvent.ACTION_UP 触发点击事件

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {

    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getAction();

        if (((viewFlags & CLICKABLE) == CLICKABLE ||
                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
                (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {
            switch (action) {
                case MotionEvent.ACTION_UP:
                                if (!post(mPerformClick)) { 
                                    performClick();
                                }
            }
        }
    }
}

4.View.post

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {
    private PerformClick mPerformClick;

    public boolean post(Runnable action) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.post(action);
        }
        // Assume that post will succeed later
        ViewRootImpl.getRunQueue().post(action);
        return true;
    }

    private final class PerformClick implements Runnable {
        @Override
        public void run() {
            performClick();
        }
    }
}

5.View.performClick 

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {

    /**
     * Call this view's OnClickListener, if it is defined.  Performs all normal
     * actions associated with clicking: reporting accessibility event, playing
     * a sound, etc.
     *
     * @return True there was an assigned OnClickListener that was called, false
     *         otherwise is returned.
     */
    public boolean performClick() {
        final boolean result;
        final ListenerInfo li = mListenerInfo;
        if (li != null && li.mOnClickListener != null) {
            playSoundEffect(SoundEffectConstants.CLICK);
            li.mOnClickListener.onClick(this);  //执行点击事件
            result = true;
        } else {
            result = false;
        }

        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
        return result;
    }
}

你可能感兴趣的:(Android,android)