Android event transfer mechanism [Touch event]

Source: Internet
Author: User

 

Android provides three levels of touch event processing: viewgroup, view, and activity. That is to say, there are Event Callback methods in all three parts.

 

Test the demo view structure:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Com. Orgcent. eventtest. eventlinearlayout
Xmlns: Android=Http://schemas.android.com/apk/res/android"
Android: layout_width="Fill_parent"
Android: layout_height="Fill_parent"
Android: Background="# 032d3d"
Android: Orientation="Vertical" >
<Com. Orgcent. eventtest. eventtextview
Android: ID="@ + ID/tvevent"
Android: layout_width="Fill_parent"
Android: layout_height="100dp"
Android: gravity="Center"
Android: textcolor="@ Android: color/Black"
Android: Background="@ Android: color/White"
Android: Text="Activity-viewgroup-view event http://orgcent.com dispatch test"/>
</COM>

 

The execution sequence of the three elements is described in detail below:

 

In general, the event Transmission sequence is viewgroup: onintercepttouchevent ()-> viewgroup or view ontouchevent ()-> activity: ontouchevent ()

The returned values of each Event Callback method are different, causing subtle changes to the event itself and its sequence. The following describes the returned values in detail:

 

Note the following:
1. If onintercepttouchevent () returns true, this method only intercepts the action_down action.
2. onintercepttouchevent () is responsible for event distribution (event transfer direction), and ontouchevent () is responsible for event processing (consumption ).

 

1. onintercepttouchevent () of viewgroup ()
Returns false:
Default implementation method. Events (press, move, and lift) are directly transmitted to the target view (User-touched view ).
When triggered in viewgroup, call viewgroup: ontouchevent (). When triggered in view, call view: ontouchevent ().

 


PS: viewgroup: ontouchevent () is also called because view: ontouchevent () does not process this event (false is returned) and the event is handed over to the parent container for processing.

 

Returns true:
The viewgroup intercepts the touch event of the Child view. The event is directly transmitted to viewgroup: ontouchevent () for processing.
That is to say, the movement and lifting actions behind the event do not pass through onintercepttouchevent (), but directly pass to ontouchevent ().

 

 

2. ontouchevent () of viewgroup/View ()
Returns true:
It indicates that the event is processed by the press action, which means that the subsequent actions such as moving and lifting of the event will be passed to this method.
For view processing, the ontouchevent () of viewgroup will not obtain the event.

 


PS: As long as onintercepttouchevent () returns false, and the target control view: ontouchevent () returns true, every action of the event (press, move, lift, etc) will be passed to onintercepttouchevent () first.

 

If viewgroup is used for processing, the activity will not obtain the event.

 

Returns false:
The view or viewgroup does not process the event. The system will pass the event to its parent for processing.

 

If the view returns false, it is processed by its parent container viewgroup. If viewgroup is not processed, it will be handed over to activity for processing.

 

If viewgroup returns false, it is handed over to the last-level activity for processing.

 

3. ontouchevent () of the activity ()
This method is the last place where the event is processed. If not, the system will discard the event. No response to this method is foundProgramWhat is the significance. If true is returned, the system event is handled.

 

TIPS:
Do not directly call the Event Callback method in the program. You can use dispatchtouchevent (motionevent) to distribute events.

 

Demo:Android_event_test.zip (56.8 kb, 47 times)

 

For reprint, enter the address:Http://orgcent.com/android-touch-event-mechanism/ | radish cabbage blog

 


 

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.