Android event interception Processing Mechanism

Source: Internet
Author: User

Android event interception Processing Mechanism

Some time ago, I have been familiar with android mobile phone development, but I am not very familiar with its event propagation mechanism. Although I have also checked the relevant information on the Internet, I always feel that the understanding is vague, so I wrote a small demo and tested it myself. Finally, I understood its specific mechanism. Write down your own conclusions and share them with anyone who wants to help android beginners.

Layout effect:

Figure 1

 

Let's take a look at the specific conclusions:

1) onInterceptTouchEvent intercepts touch events. For nested views, the onInterceptTouchEvent method is the onInterceptTouchEvent method of the view on the outermost layer, and then the onInterceptTouchEvent of the subview is executed in sequence, then, execute the subview event Interception Method in the subview (assuming that the onInterceptTouchEvent of all nested views will be executed here, so that the onInterceptTouchEvent of each view can return false ). Therefore, the onInterceptTouchEvent execution sequence is A ---> B ---> C ---> D. That is, it is transmitted from the parent View to the Child view. In short, the event interception mechanism is initiated by the parent view to intercept the event (when an accident occurs, Lao Tzu first, son later ). When A finger-touch event is referenced, parent view A first intercepts the event. If A fails to intercept the event, it is intercepted by its child View B; if B fails to intercept, it will be handed over to B's subview C for intercept .. until a subview successfully blocks the event.

2) Whether a view intercepts an event successfully or not is identified as the return value of the onInterceptTouchEvent method. If true is returned, the interception succeeds. If false is returned, the current view fails to intercept the event.

3) Let's talk about the successful interception. Suppose view C successfully intercepts the current touch event. Successful interception means that the event will not be passed to view D. Therefore, the onInterceptTouchEvent of the d view cannot be run at this time (the event cannot be reached, but who else can intercept it ?). After the event is intercepted successfully, the event will be processed immediately. The onTouchEvent method is used to process the event. At this time, view C is intercepted successfully, and then the onTouchEvent method of view C is executed. Does this mean that the current touch event is handled by the onTouchEvent method of view C? This is determined by the return value of the onTouchEvent method in the C view. When onTouchEvent of the c view returns true, the current event is handled by C. Of course, the action of the event and the MotionEvent are handled. ACTION_MOVE and ACTION_UP are all handed over to the onTouchEvent method of c for processing. Therefore, you can switch (event. getAction) in the onTouchEvent method of c to determine the execution logic. If false is returned, the C view does not process this event or cannot process it. What should I do? If my son doesn't work, dad will come, and the event will be handed over to the onTouchEvent method of View B. Similarly, whether B handles this event depends on the return value of B's onTouchEvent. The specific explanation is the same as that of C.

4) When both onInterceptTouchEvent and onTouchEvent of a B C D Return false, the execution sequence of the methods is. onInterceptTouchEvent --> B. onInterceptTouchEvent --> C. onInterceptTouchEvent --> D. touchEvent (the deepest subview does not overwrite onInterceptTouchEvent) --> C. touchEvent --> B. touchEvent -->. touchEvent. that is to say, the interception event is intercepted by the parent view with the child view with priority.

Conclusion: onInterceptTouchEvent intercepts the event. After successful interception, it is handed over to the view that first encounters onTouchEvent and returns true for processing.

The following describes in detail how the above conclusions are obtained. The preparation is divided into two parts for a step-by-step explanation. If you have understood the above, you should not read the following content, because it will be awkward.

 

 

The last D is A custom TextView. The difference between D and a B C is that D only overrides the onTouchEvent method, the three custom controls a B C also override the onInterceptEvent method.

The Code of D is as follows. The Code of a B C is basically the same except for the class name and output log, so only one of them is pasted to Reduce the number.

DView code:

 

public class DView extends TextView{    private static String tag = D;public DView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public DView(Context context, AttributeSet attrs) {super(context, attrs);}public DView(Context context) {super(context);}  @Overridepublic boolean onTouchEvent(MotionEvent event) {Log.e(tag, --onTouchEvent--D);return false;}}

The AView code is similar to that of c d:

 

 

public class AView extends RelativeLayout{    private static String tag = A;public AView(Context context) {super(context);}public AView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public AView(Context context, AttributeSet attrs) {super(context, attrs);} @Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {Log.e(tag,--onInterceptTouchEvent--A);return false;}@Overridepublic boolean onTouchEvent(MotionEvent event) {Log.e(tag,--onTouchEvent---A );return false;}}

 

At the beginning, all the methods to be rewritten return false. The output log of running click is:

Convert:

 

 

As shown in the following figure, the execution sequence of onInterceptTouchEvent events is from the parent control to the Child control, and takes precedence over the onTouchEvent method of the control, the execution sequence of onTouchEvent events is the opposite: Child control to parent control. Note: Because false is returned at this time, no view is used to process each ACTION of the touch event. This is why the onTouchEvent is always passed to. Therefore, events such as ACTION_MOVE and ACTION_UP cannot be processed accordingly. In this case, even if you write the following code in the onTouchEvent method of D, it will not be executed.

 

if(event.getAction()==MotionEvent.ACTION_MOVE){Log.e(tag, --onTouchEvent--*****);}

 

1) if the InterceptTouchEvent of A returns true, And the rest still returns false, the output log is:

 

 

Convert:


 

It can be found that A intercepts this Touch event, and the event is no longer transmitted to the Child control B C D of. At this time, all action events, such as the finger movement event ACTION_MOVE or ACTION_UP event, are handed over to the onTouchEvent method of a for processing (of course, this is when the onTouchEvent method returns true, if the return value is false, these actions will not be taken after testing ). The B, C, and D controls are the event processing interception methods and event processing methods that cannot be executed.

 

2) only when the onIntercepteTouchEvent of B returns true, the printed log is

 

Convert:

 

At this time, B intercepts this Touch event and does not pass it to the C D subcontrol. Similarly, because the onTouchEvent event returns false, the event of this event. all actions of getAction () are not processed.

 

4) Likewise, we can see that when the onIntercept method of the c control returns true, while the others still return false, the output log is

 

Convert

The following describes how onTouchEvent of each view returns true.

Because the onTouchEvent event is transmitted from the Child control to the parent control, when onTouchEvent of D returns true, the test output result is as follows:

Convert:

After testing, it is found that D processes various actions of the Touch event, and c B D is onTouchEvent not executed.

Similarly, when the onTouchEvent method of c returns true, the output log is as follows:

Convert to the following:

After testing, it is found that each action of the event is responded to in the onTouchEvent method of CView, while the onTouchEvent of D does not correspond to MotionEvent. ACTION_XX. The rest of the cases will be no longer long. After a step-by-step test, it is concluded that the ending article at the beginning of the article is a little too long. I hope it will be helpful to those who read this article.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.