Android Touch event distribution mechanism fully resolves "one"

Source: Internet
Author: User

Recently in the high-D map, due to user requirements, have to use scrollvew nested Mapview, although the official request does not recommend doing so, but also forced to helpless ... The devil is a tall foot, the road Gao Zhang. There is something that programmers can't solve, if there is a solution two times.

Given the use of touch events, the Android touch event mechanism is summed up.

First, when the user is doing screen operation, there are two cases

    • One is the key event

    • Second, touch events

Keystroke events are divided into long press and click events, which are too simple to summarize here.

Touch events

The composition of touch events:
-A Actiondown
-N A Actionmove
-A Actionup
-An OnClick
-A Onlongclick
-A Onscroll

Android components
    • Inherit Viewgroupo like Linearlayout,scrollview,gridview,extends–>viewgroup's view
    • ViewGroup container
    • Inherited from view does not include other view, such as Textview,edittext,butto, etc.

Here is a good one to talk about a blog address:

http://www.trinea.cn/android/touch-event-delivery-mechanism/

Motionevent

All touch events are encapsulated as motionevent objects, including touch location, time, history, and the first few fingers (multi-fingered touch).

Classification of the processing of Android events
    • Distribution (many are also known as passing events): dispatchtouchevent function

    • Consumption: Ontouchevent functions and Ontouchlistener functions

    • Intercept: dispatchtouchevent function

We all know that the touch events of Android are passed from the outer layers to the inner layer: the outermost activity-->viewgroup--> viewgroupo-->.......view.

First touch event delivery must be activity at the beginning;

The second way of transmission is through the tunnel way;

The third is passed to the outermost view, which is the top view, which is distributed by this method of view.

For activity:

    @Override    publicbooleandispatchTouchEvent(MotionEvent ev) {        booleansuper.dispatchTouchEvent(ev);        return isFlag;    }    @Override    publicbooleanonTouchEvent(MotionEvent event) {        booleansuper.onTouchEvent(event);        return isFlag;    }

For ViewGroup:

  @Override     Public Boolean ontouchevent(Motionevent event) {BooleanIsflag =Super. Ontouchevent (event);returnIsflag; }@Override     Public Boolean onintercepttouchevent(Motionevent event) {BooleanIsflag =Super. Onintercepttouchevent (event);returnIsflag; }@Override     Public Boolean dispatchtouchevent(Motionevent event) {BooleanIsflag =Super. Dispatchtouchevent (event);returnIsflag; }

For view, as with activity, only consumption and interception

    @Override    publicbooleandispatchTouchEvent(MotionEvent ev) {        booleansuper.dispatchTouchEvent(ev);        return isFlag;    }    @Override    publicbooleanonTouchEvent(MotionEvent event) {        booleansuper.onTouchEvent(event);        return isFlag;    }

Http://www.open-open.com/lib/view/open1463016891525.html

The touch events in Android start with Action_down:

Single finger operation: action_down-action_move--action_up

Multi-finger operation: action_down-action_pointer_down-action_move–action_pointer_up-action_up.

If you do not intercept, do not consume the basic flowchart:

Here we use the case analysis:

activity--"ViewGroup (mylinearlayout)-" ViewGroup (Mysubview)

The Mylinearlayout Dispatchtouchevent returns false.

Printing results:

  • Process and Result analysis:

  • The event is first distributed to the dispatchtouchevent of the Toucheventfather control by the Toucheventactivity dispatchtouchevent method;

  • The dispatchtouchevent of the Toucheventfather control returns false, indicating that the acquired event is stopped and the event is not consumed;

  • Since the event acquired by Toucheventfather comes directly from Toucheventactivity, the event is returned to Toucheventactivity's ontouchevent for consumption;

  • Finally, the toucheventactivity directly responds to finger movement and lifting events.

The Mylinearlayout Dispatchtouchevent returns True.

Printing results:

  • Process and Result analysis:

  • The event is first distributed to the dispatchtouchevent of the Toucheventfather control by the Toucheventactivity dispatchtouchevent method;

  • The dispatchtouchevent of the Toucheventfather control returns True, indicating that the distribution event is to the Toucheventfather control and consumed by the control's dispatchtouchevent;

  • And because toucheventactivity constantly distributes events to the Toucheventfather control's dispatchtouchevent, Toucheventfather controls Dispatchtouchevent It will also continue to consume the acquired events.

The Mylinearlayout Onintercepttouchevent returns True.

Printing results:

  • Process and Result analysis:

  • The event is first distributed to the dispatchtouchevent of the Toucheventfather control by the Toucheventactivity dispatchtouchevent method;

  • The dispatchtouchevent of the Toucheventfather control returns super.dispatchtouchevent (EV), indicating that the event is distributed and passed down to the Toucheventfather control's Onintercepttouchevent method;

  • The method returns true to intercept the event that was obtained and to pass the event to the Toucheventfather control's ontouchevent for processing, toucheventfather the ontouchevent of the control returns Super.ontouchevent (EV) indicates that the event is not handled directly by returning the event to the parent control;

  • Because Toucheventfather gets an event directly from Toucheventactivity, the ontouchevent of the Toucheventfather control returns the event in a bubbling manner to the Toucheventactivity of the ontouchevent for consumption;

  • Subsequent events will skip Toucheventfather directly from the toucheventactivity ontouchevent to consume events distributed by Toucheventactivity itself.

The Mylinearlayout Onintercepttouchevent returns false.

Printing results:

  • Process and Result analysis:

  • The event is first distributed to the dispatchtouchevent of the Toucheventfather control by the Toucheventactivity dispatchtouchevent method;

  • The control's dispatchtouchevent returns Super.dispatchtouchevent (EV), which means that the event is distributed and passed down to the Toucheventfather control onintercepttouchevent Method

  • The method returns false to indicate that the event will be released and passed to the Dispatchtouchevent method of the child control toucheventchilds;

  • The same toucheventchilds dispatchtouchevent returns super.dispatchtouchevent (EV), indicating that the event is distributed and passed down to the Toucheventchilds control's Onintercepttouchevent method;

  • The Toucheventchilds Onintercepttouchevent method returns super.onintercepttouchevent (EV), which by default passes the event to the Toucheventchilds Ontouchevent for processing;

  • The Toucheventchilds ontouchevent return super.ontouchevent (EV) means that the event is not processed directly to the parent control;

  • Because Toucheventchilds gets an event that comes directly from Toucheventfather, the ontouchevent of the Toucheventchilds control returns the event to the Toucheventfather directly in a bubbling manner. Ontouchevent for consumption;

  • The ontouchevent of Toucheventfather also returns to Super.ontouchevent (EV), and the ontouchevent of Toucheventfather also returns the event to the upper control;

  • The Toucheventfather gets an event that comes directly from toucheventactivity, so the ontouchevent of the Toucheventfather control returns the event to the toucheventactivity directly in a bubbling manner Ontouchevent for consumption;

  • Subsequent events will skip Toucheventfather and toucheventchilds directly from Toucheventactivity's ontouchevent consumption of events distributed from Toucheventactivity itself.

The Mylinearlayout Onintercepttouchevent returns false. Mysubview returns True

Printing results:

  • Process and Result analysis:

  • The event is first distributed to the dispatchtouchevent of the Toucheventfather control by the Toucheventactivity dispatchtouchevent method;

  • The control's dispatchtouchevent returns Super.dispatchtouchevent (EV), and the event is distributed to the onintercepttouchevent of Toucheventfather, and this method returns false Express the release yourselves event;

  • The event is passed to the Dispatchtouchevent method of the child control Toucheventchilds, and Dispatchtouchevent returns true to indicate that the event was distributed to Toucheventchilds and by the Dispatchtouchevent method consumption;

  • Subsequent events will continue to repeat the above logic and ultimately be consumed by the Toucheventchilds dispatchtouchevent.

In fact, if you look at the source code is more complex, summed up, the basic rules are:
    • The down event is first passed to the Onintercepttouchevent () method

    • If the ViewGroup onintercepttouchevent () return false after receiving the down event processing, the internal view will also get to down, then the subsequent move, The up and other events will continue to be passed to the viewgroup and then passed to the ontouchevent () processing of the final target view.

    • If the ViewGroup onintercepttouchevent () return true after receiving the down event processing, the internal view will not be on delivery, the subsequent move, The up and other events are no longer passed to Onintercepttouchevent (), and of course, Move,up is not passed to the view

    • If the ontouchevent () of the view that eventually needs to handle the event returns false, the event is passed to the ontouchevent () processing of the view at its previous level.

    • If the ontouchevent () of the view that eventually needs to handle the event returns TRUE, then subsequent events will continue to be passed to the view's ontouchevent () processing.

    • Group's touch has nothing to do with this mechanism, it has to do with the upper level.

From Androidstarjack Blog Address:

http://blog.csdn.net/androidstarjack/article/details/71910168

If you think this article is helpful to you, welcome to QQ Group: 232203809??

Public Number: Terminal Research and Development department

(Welcome to learn and Exchange)

Android Touch event distribution mechanism fully resolves "one"

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.