Android Event-passing mechanism

Source: Internet
Author: User
Tags continue

Experimental environment

OS X 10.9

Eclipse (ADT)

Android Source version: API level (Android 4.4)

Android Event composition

In Android, events include Dots, long press, drag, slide, etc., click and double-click, and also include a single point of operation and multiple-fingered operation. All of this constitutes an event response in Android. In general, all events are based on the following three sections:

Press (Action_down)

Move (Action_move)

Lift Up (ACTION_UP)

All action events must first be performed by pressing the action (Actiondown), after which all operations are based on a press operation, and when the press operation completes, the next step may be a move (actionmove) and then lift (ACTION_UP), Or you can lift it directly after the operation is done without moving. This series of actions can be controlled in Android.

We know that all of the event operations happen on the touchscreen, and the on-screen interaction with us is a variety of view components (view), in which all views are inherited from view and the view is laid out through various layout components (ViewGroup). ViewGroup also inherits from view. All UI controls, such as button, TextView, are inherited from view, and all layout controls such as Relativelayout, container controls, such as ListView, inherit from ViewGroup. So, the main thing about our event operations is between view and ViewGroup, so what are the main ways to respond to these events in view and ViewGroup? Remember the following 3 methods, which we can see by looking at the source code of view and ViewGroup:

View.java

public boolean dispatchtouchevent (Motionevent event)

Viewgroup.java

Ublic boolean dispatchtouchevent (Motionevent event) public
Boolean ontouchevent (Motionevent event) 
public Boolean onintercepttouchevent (Motionevent event)

There are dispatchtouchevent and Ontouchevent methods in both view and ViewGroup, but in ViewGroup there is a onintercepttouchevent method, so what are these methods? Don't worry, let's take a look at their return value first. The return values of these methods are all Boolean, why is the Boolean, look at the title of this article, "Event delivery", the process of passing is one after another, that to a certain point will continue to pass down after it? Did you find that the word "whether" is determined by Boolean as the return value. Yes, all of these methods return True or FALSE. In Android, all events are consumed from the start through to the completion of events, and the return value of these methods determines whether an event continues to be transmitted, intercepted, or consumed.

The next step is the parameters of these methods, which accept a motionevent type of argument, Motionevent inherited from InputEvent, to mark various action events. The previously mentioned Actiondown, Actionmove, and action_up are constants defined in Motinevent. We judge which type of event is received by motionevent the type of event passed in. Up to now, the return values and parameters of these three methods you should all understand, and then explain how each of these three methods handles the event.

The Dispatchtouchevent method is used for the distribution of events, all events in Android must be distributed by this method, and then decide whether to consume the current event itself or continue to distribute it to the child control process. Returns true to indicate that no distribution has been continued and that the event has not been consumed. return false to continue distributing, and if it is viewgroup, distribute to onintercepttouchevent to determine whether to intercept the event.

The Ontouchevent method is used for handling the event, returning true to indicate that the consumption is handling the current event, returning false to not processing, and giving the child control a continuation distribution.

Onintercepttouchevent is a ViewGroup method, the view does not, its role is responsible for the interception of events, return True when the interception of the current event, do not continue to distribute, to their own ontouchevent for processing. Returns false without intercepting, and continues to pass down. This is a viewgroup-specific approach because there may be a child view in the ViewGroup, and the view in Android can no longer contain child view (iOS).

So far, the composition of Android events and the role of event handling methods you should be more clear, and then we will use a demo to actually experience the experiment.

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.