[Android] Zhaohuaxishi Custom View chapter of the (five) Android event distribution mechanism (in) distribution logic and some "strange" phenomenon wonderful experience from source to analyze the event

Foreword

       In the previous article [ [Android] from Zhaohuaxishi (e) Android event distribution mechanism (on) the definition of the processing logic of Touch View chapter three important methods ], we have by way of example and log events to analyze the Android distribution mechanism. These, we just saw the phenomenon, if you want to learn more about the event distribution mechanism, this is not enough, we also need to look beyond the surface, to study research source. This article from the source (based on Android API-26) starting to analyze the previous article we see the phenomenon, as well as other events and related frequently asked questions, such as why the requestDisallowInterceptTouchEvent failure? view set focusable = "false", why can trigger a click event? Touch the event and the Click event Backwards? and many more!

 

A, Activity processing logic of events

       In the sample code on an article in our Boss - EventDemoActivity class have the following code

 1 //=============Boss:EventDemoActivity.java============
 2 @Override
 3     public boolean dispatchTouchEvent(MotionEvent ev) {
 4         Log.i("songzheweiwang", "[EventDemoActivity-->dispatchTouchEvent]ev=" + EventUtil.parseAction(ev.getAction()));
 5         return super.dispatchTouchEvent(ev);
 6     }
 7 
 8     @Override
 9     public boolean onTouchEvent(MotionEvent event) {
10         Log.i("songzheweiwang", "[EventDemoActivity-->onTouchEvent]event=" + EventUtil.parseAction(event.getAction()));
11         return super.onTouchEvent(event);
12     }

      Activity is the starting point of the event, dispatchTouchEvent is where the whole logic of the first distribution event. But not View System Activity in one, then it is how the events are distributed to View System do? Super.dispatchTouchEvent tracking method, the method will enter the Activity.java

 //==========================Activity.java=======================
1
/** 2 * Called to process touch screen events. You can override this to 3 * intercept all touch screen events before they are dispatched to the 4 * window. Be sure to call this implementation for touch screen events 5 * that should be handled normally. 6 * 7 * @param ev The touch screen event. 8 * 9 * @return boolean Return true if this event was consumed. 10 */ 11 public boolean dispatchTouchEvent(MotionEvent ev) { 12 if (ev.getAction() == MotionEvent.ACTION_DOWN) { 13 onUserInteraction(); 14 } 15 if (getWindow().superDispatchTouchEvent(ev)) { 16 return true; 17 } 18 return onTouchEvent(ev); 19 }

 

 1 //=============Activity.java==========
 2 ......
 3 private Window mWindow;
 4 ......
 5 public Window getWindow() {
 6         return mWindow;
 7     }
 8 ......
 9 
10 //================Window.java================
11 /**
12  * ......
13  * <p>The only existing implementation of this abstract class is
14  * android.view.PhoneWindow, which you should instantiate when needing a
15  * Window.
16  */
17 public abstract class Window {
18   ......
19     public abstract boolean superDispatchTouchEvent(MotionEvent event);
20   ......
21 }

 

 1 //=============PhoneWindow.java==========
 2 ......
 3 // This is the top-level view of the window, containing the window decor.
 4 private DecorView mDecor;
 5 ......
 6 @Override
 7 public boolean superDispatchTouchEvent(MotionEvent event) {
 8     return mDecor.superDispatchTouchEvent(event);
 9 }
10 
11 //===========DecorView.java==========
12 ......
13 public boolean superDispatchTouchEvent(MotionEvent event) {
14     return super.dispatchTouchEvent(event);
15 }
16 ....
17 
18 //========ViewGroup.java=======
19 @Override
20 public boolean dispatchTouchEvent(MotionEvent ev) {
21 ......
22 }

 

Guess you like

Origin www.cnblogs.com/andy-songwei/p/11015293.html