從源碼角度帶你分析 Android View 事件分發 dispatchTouchEvent,onTouch,onTouchEvent,onClick邏輯順序過程

我們先從一個例子看起,先重寫一個MyButton 繼承Button,代碼如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class MyBtn extends Button {
 
     public MyButton(Context context) {
         super (context);
     }
 
     public MyBtn(Context context, AttributeSet attrs) {
         super (context, attrs);
     }
 
     public MyBtn(Context context, AttributeSet attrs, int defStyleAttr) {
         super (context, attrs, defStyleAttr);
     }
 
 
     @Override
     public boolean dispatchTouchEvent(MotionEvent event) {
 
         switch (event.getAction()) {
             case MotionEvent.ACTION_DOWN:
                 MyLog.e( "dispatchTouchEvent====MyButton=====ACTION_DOWN" );
                 break ;
             case MotionEvent.ACTION_MOVE:
                 MyLog.e( "dispatchTouchEvent====MyButton=====ACTION_MOVE" );
                 break ;
             case MotionEvent.ACTION_UP:
                 MyLog.e( "dispatchTouchEvent====MyButton=====ACTION_UP" );
                 break ;
         }
 
         return super .dispatchTouchEvent(event);
     }
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
 
         switch (event.getAction()) {
             case MotionEvent.ACTION_DOWN:
                 MyLog.e( "onTouchEvent====MyButton=====ACTION_DOWN" );
                 break ;
             case MotionEvent.ACTION_MOVE:
                 MyLog.e( "onTouchEvent====MyButton=====ACTION_MOVE" );
                 break ;
             case MotionEvent.ACTION_UP:
                 MyLog.e( "onTouchEvent====MyButton=====ACTION_UP" );
                 break ;
         }
 
         return super .onTouchEvent(event);
     }

 

 

佈局文件如下:

 

?
1
2
3
4
5
<relativelayout android:layout_height= "match_parent" android:layout_width= "match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= ".MainActivity" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" >
 
     <com.xjp.testtouchevent.mybutton android:id= "@+id/myButton" android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:text= "測試" >
 
</com.xjp.testtouchevent.mybutton></relativelayout>


測試Activity如下:

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class MainActivity extends ActionBarActivity {
 
     private Button myButton;
 
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         myButton = (Button) findViewById(R.id.myButton);
 
         myButton.setOnTouchListener( new View.OnTouchListener() {
             @Override
             public boolean onTouch(View v, MotionEvent event) {
                 switch (event.getAction()) {
                     case MotionEvent.ACTION_DOWN:
                         MyLog.e( "onTouch====MyButton=====ACTION_DOWN" );
                         break ;
                     case MotionEvent.ACTION_MOVE:
                         MyLog.e( "onTouch====MyButton=====ACTION_MOVE" );
                         break ;
                     case MotionEvent.ACTION_UP:
                         MyLog.e( "onTouch====MyButton=====ACTION_UP" );
                         break ;
                 }
                 return false ;
             }
         });
 
         myButton.setOnClickListener( new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 MyLog.e( "onClick====MyButton=====onClick" );
             }
         });
 
 
     }
 
 
}

點擊測試按鈕,打印結果如下:

 

\

我們從打印結果可以直觀看到,點擊Button按鈕事件分發過程如下 dispatchTouchEvent---->onTouch---->onTouchEvent----->onClick。並且如果仔細的你會發現,都是在ACTION_UP事件之後才觸發onClick點擊事件,爲什麼會是這樣??現在我們不得而知。我們僅僅是從打印結果推測事件分發的結論,現在我們從源碼分析下這個事件分發流程爲什麼是這樣子。

 

事件分發都是從dispatchTouchEvent方法開始的,那麼我們這裏是重寫了dispatchTouchEvent方法,並且最後也調用了父類的super.dispatchTouchEvent(event)方法。那麼我們看看父類中的方法到底做了什麼??點擊進入父類的dispatchTouchEvent方法,發現此方法在View類中找到,其實也不奇怪,所有控件的父類都是View。這裏我貼出最新源碼如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public boolean dispatchTouchEvent(MotionEvent event) {
     boolean result = false ;
 
     if (mInputEventConsistencyVerifier != null ) {
         mInputEventConsistencyVerifier.onTouchEvent(event, 0 );
     }
 
     final int actionMasked = event.getActionMasked();
     if (actionMasked == MotionEvent.ACTION_DOWN) {
         // Defensive cleanup for new gesture
         stopNestedScroll();
     }
 
     if (onFilterTouchEventForSecurity(event)) {
         //noinspection SimplifiableIfStatement
         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 ;
         }
     }
 
     if (!result && mInputEventConsistencyVerifier != null ) {
         mInputEventConsistencyVerifier.onUnhandledEvent(event, 0 );
     }
 
     // Clean up after nested scrolls if this is the end of a gesture;
     // also cancel it if we tried an ACTION_DOWN but we didn't want the rest
     // of the gesture.
     if (actionMasked == MotionEvent.ACTION_UP ||
             actionMasked == MotionEvent.ACTION_CANCEL ||
             (actionMasked == MotionEvent.ACTION_DOWN && !result)) {
         stopNestedScroll();
     }
 
     return result;
}


忽略其他無關代碼,我們直接看17--25行。第17行的if判斷關鍵在於li.mOnTouchListener.onTouch(this, event) 的返回值,這個接口回調就是我們外面寫的myButton.setOnTouchListener事件(Button 的onTouch事件),在MainActivity代碼裏,我們setOnTouchListener返回的值是false,所以在源碼中我們可以看到 17行的條件不成立,那麼條件不成立,result=false;因此,源碼的第23行if 判斷第一個條件成立,繼續執行第二個條件,也就是onTouchEvent。我們跳到這個方法裏看看裏面幹啥了?看如下代碼:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)) {
                                     performClick();
                                 }
                             }
                         }
 
                         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 ;
     }

我們看看這裏邊都做了些什麼,忽略其他,我們直接看37行的 performClick(); 方法,跳進去繼續看,(注意:這裏的performClick方法是在ACTION_UP手勢裏邊執行的哦!!!)

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
     }
看見沒??第6行 li.mOnClickListener.onClick(this); 這個接口回調就是我們Button的 onClick事件。到此爲止,我們從源碼分析了Button事件分發過程
結論:dispatchTouchEvent---->onTouch---->onTouchEvent----->onClick。並且如果仔細的你會發現,是在所有ACTION_UP事件之後才觸發onClick點擊事件。

 

現在我們來看看其他情況:當onTouch返回爲true,打印結果如下:

\

驚奇的發現,竟然沒有執行onClick事件是吧????如果你仔細閱讀上面的文章,估計你知道爲什麼了吧?還是跟大家一起分析一下吧:源碼如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public boolean dispatchTouchEvent(MotionEvent event) {
        boolean result = false ;
 
        if (mInputEventConsistencyVerifier != null ) {
            mInputEventConsistencyVerifier.onTouchEvent(event, 0 );
        }
 
        final int actionMasked = event.getActionMasked();
        if (actionMasked == MotionEvent.ACTION_DOWN) {
            // Defensive cleanup for new gesture
            stopNestedScroll();
        }
 
        if (onFilterTouchEventForSecurity(event)) {
            //noinspection SimplifiableIfStatement
            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 ;
            }
        }
 
        if (!result && mInputEventConsistencyVerifier != null ) {
            mInputEventConsistencyVerifier.onUnhandledEvent(event, 0 );
        }
 
        // Clean up after nested scrolls if this is the end of a gesture;
        // also cancel it if we tried an ACTION_DOWN but we didn't want the rest
        // of the gesture.
        if (actionMasked == MotionEvent.ACTION_UP ||
                actionMasked == MotionEvent.ACTION_CANCEL ||
相關文章
相關標籤/搜索