Android - 事件传递机制

一  dispatchTouchEvent()

对于 View 来说,事件传递的入口方法是 dispatchTouchEvent() ,它主要负责分发事件。

<span style="font-size:14px;">public boolean dispatchTouchEvent(MotionEvent event) {  
        if (mInputEventConsistencyVerifier != null) {  
            mInputEventConsistencyVerifier.onTouchEvent(event, 0);  
        }  
  
        if (onFilterTouchEventForSecurity(event)) {  
            //noinspection SimplifiableIfStatement  
            ListenerInfo li = mListenerInfo;  
           <span style="color:#3333ff;"> </span>if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED  
                    && <span style="color:#3333ff;">li.mOnTouchListener.onTouch(this, event)</span>) {  
                return true;  
            }  
  
            if (<span style="color:#ff9900;">onTouchEvent(event)</span>) {  
                return true;  
            }<span style="color:#ff9900;">  </span>
        }  
  
        if (mInputEventConsistencyVerifier != null) {  
            mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);  
        }  
        return false;  
    }</span>
dispatchTouchEvent() 源码中,先执行OnTouchListener.onTouch() ,然后执行onTouchEvent() 。


二  onTouchEvent()

<span style="font-size:14px;">public boolean onTouchEvent(MotionEvent event) {
 
        if (((viewFlags & CLICKABLE) == CLICKABLE ||
                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
                    if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
                        // take focus if we don't have it already and we should in
                        // touch mode.
                        boolean focusTaken = false;
                        if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
                            focusTaken = requestFocus();
                        }
 
                        if (prepressed) {
                            // The button is being released before we actually
                            // showed it as pressed.  Make it show the pressed
                            // state now (before scheduling the click) to ensure
                            // the user sees it.
                            setPressed(true, x, y);
                       }
 
                        if (!mHasPerformedLongPress) {
                            // This is a tap, so remove the longpress check
                            removeLongPressCallback();
 
                            // Only perform take click actions if we were in the pressed state
                            if (!focusTaken) {
                                // Use a Runnable and post this rather than calling
                                // performClick directly. This lets other visual state
                                // of the view update before click actions start.
                                if (mPerformClick == null) {
                                    mPerformClick = new PerformClick();
                                }
                                if (!post(mPerformClick)) {
                                    <span style="color:#3333ff;">performClick();</span>
                                }
                            }
                        }
 
                        if (mUnsetPressedState == null) {
                            mUnsetPressedState = new UnsetPressedState();
                        }
 
                        if (prepressed) {
                            postDelayed(mUnsetPressedState,
                                    ViewConfiguration.getPressedStateDuration());
                        } else if (!post(mUnsetPressedState)) {
                            // If the post failed, unpress right now
                            mUnsetPressedState.run();
                        }
 
                        removeTapCallback();
                    }
                    break;
            return true;
        }
 
        return false;
    }</span>

<span style="font-size:14px;">public boolean performClick() {
        final boolean result;
        final ListenerInfo li = mListenerInfo;
        if (li != null && li.mOnClickListener != null) {
            playSoundEffect(SoundEffectConstants.CLICK);
            <span style="color:#ff9900;">li.mOnClickListener.onClick(this);</span>
            result = true;
        } else {
            result = false;
        }
 
        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
        return result;
    }</span>

而在 onTouchEvent () 源码中,有对 OnClickListener.onClick() 的调用。


三  事件传递顺序

dispatchTouchEvent  -->  onTouch  -->  onTouchEvent  -->  onClick

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值