Android event transfer mechanism and onInterceptTouchEvent () and onTouchEvent () Summary

Source: Internet
Author: User
To be honest, these two little things are too troublesome and difficult to understand. I feel dizzy in my own api documentation and find a lot of information on the Internet to find out what is going on. Here I will summarize, remember this principle and it will be clear: 1. onInterceptTouchEvent () is used to process events (similar to preprocessing, but can also not be processed) and change the event transmission direction, that is, to determine whether to allow the Touch event to be passed down (sub-control), but return True (representing the event in the current viewGro

To be honest, these two little things are too troublesome and difficult to understand. I feel dizzy in my own api documentation and find a lot of information on the Internet to find out what is going on. Here I will summarize, remember this principle to be clear:
 
1. onInterceptTouchEvent () is used to process events (similar to preprocessing, but it can also not be processed) and change the event transmission direction, that is, to determine whether to allow the Touch event to continue downward (sub-Control) pass, 1 but return
 
True (events will be processed in the current viewGroup), the downward transmission path is truncated (all child controls will not be able to participate in Touch events ), at the same time, the event is passed to the onTouchEvent () processing of the current control;
 
If false is returned, the event is handed over to the onInterceptTouchEvent () of the sub-control ()
 
2. onTouchEvent () is used to process events. The returned value determines whether the current control consumes the event (consume). That is to say, after the current control finishes processing the Touch event, whether or not the Touch event is allowed to continue up (parent Control
 
If the value is True, the parent control does not have to handle the Touch event on its own. If the return value is true, it is passed up to the parent control (Note: Do you think it is related to consumption? Anyway, I have compiled a processing generation for the event
 
Code? The answer is yes! For example, the premise that ACTION_MOVE or ACTION_UP occurs is that ACTION_DOWN occurs. If you do not consume ACTION_DOWN, the system will think that ACTION_DOWN is not
 
If this happens, ACTION_MOVE or ACTION_UP cannot be captured .)

Source Code address:

Free onHttp://linux.linuxidc.com/

Both the user name and password areWww.linuxidc.com

The specific download directory is in/October 12, 2014/October 3, January/Android event transfer mechanism and onInterceptTouchEvent () and onTouchEvent () Summary

Download Method see http://www.linuxidc.com/Linux/2013-07/87684.htm

 

General rules:

1. The onInterceptTouchEvent contains an Intercept. What does this mean? Her name is interception. You probably know her role. She is your kiss secretary. If you want to handle anything, contact the Secretary first. The secretary decides if you want to handle it yourself, as a secretary, can everyone have it? Of course not. This Secretary only belongs to viewgroup, so only controls inherited from viewgroup are available. Those textviews, buttons, and so on certainly do not exist. Why? Because his level is not enough (inherited from the view), there cannot be secretaries. Note that if the leaders at the level decide to proceed, the subsequent notifications will not pass through the secrets at this level, send leaders directly.

2. the event Transfer sequence occurs in sequence when ACTION_DOWN is passed from the parent control to the Child control, then ACTION_MOVE (may or may not appear), and finally ACTION_UP, note that the parent control is passed to the Child control in sequence. ACTION_MOVE and ACTION_UP on the mobile phone will basically appear because the sensor on the mobile phone is very sensitive, but the simulator is different. If the mobile phone does not slide, action_MOVE is not displayed. All events, ACTION_DOWN, ACTION_MOVE, and ACTION_UP are first sent from the onInterceptTouchEvent of the root control (the most external layer in the layout file). That is to say, each control will first receive the onInterceptTouchEvent event (of course, you must have the ability to receive the event because of the above reasons)

3. if there is a secret and an event, there should be a leader. onTouchEvent is the leader who handles the specific event. The leader will first receive the ACTION_DOWN event sent by the secret, (1) Wow, the secret sent me. Hurry up and check if there is any activity in the evening. You can take the secret out of the room and enjoy it, return true: after attending the evening event, the upper-level secret will send the specific schedule (ACTION_MOVE and ACTION_UP) of the event, which will be handled by the lead (note, for specific activities, the upper-level secrets directly inform the lower-level leaders (OnTouchEvent). Here, the lower-level secrets are no longer required to be filtered out, because the lower-level secrets are dressed up and will accompany the leaders in the evening.) What about other leaders, I can only sigh with hope.

(2) If the leaders see it and find that it is not about going out to be happy, they are in a depressed mood and cannot join the secret. This Nima's is still going to be Mao! No !!! Tell the Sub-Secretary (return false) not to handle it, and then the Sub-Secretary will report to the upper-level (parent control), and then the upper-level leaders will handle it. Shenma? If the superior leads do not handle the issue, let's rely on it. Well, all of them are handed over to their ancestor (View) to view the View source code. You will find that, in the same way, if you accept the event, true will be returned, then process ACTION_MOVE and ACTION_UP. If no action is processed, false is returned. If no action is processed at the ancestor level, the event disappears. Prepare to process the next one.


Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center">
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/TV"
Android: text = "AB"
Android: textSize = "40sp"
Android: textStyle = "bold"
Android: background = "# FFFFFF"
Android: textColor = "# 0000FF"/>

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.