Control touch event sorting

Source: Internet
Author: User

About touch event transfer: MotionEvent is generated when the Activity contacts the screen. The transfer order is as follows:

First, trigger the dispatchTouchEvent of the Activity.

Then, the onUserInteraction of the Activity is triggered.

Then, the Layou dispatchTouchEvent is triggered.

Then, the onInterceptTouchEvent (onTouchevent) of Layout is triggered)

Then, the View's dispatchTouchEvent is triggered.

Then, the onTouchEvent of the View is triggered.

Note the usage of Layout onInterceptTouchEvent and onTouchEvent, and View dispatchTouchEvent and onTouchEvent.


For onInterceptTouchEvent, the source code of onInterceptTouchEvent is only one "return false". If you need to block the MontionEvent of this layer, it should overwrite and return true. At this time, the onTouchEvent of this Layout will be triggered and will not be passed, remember that either View or ViewGroup, once onTouchEvent is triggered, this MontionEvent cannot be passed down,

For onTouchEvent, this function is the key to triggering other Listener, such as click, but it does not include onTouchListener. Generally, it should not be completely overwritten and Super should be called in a timely manner. onTouchEvent. The returned value indicates that if true is returned, the action between MontionEventdown and up will be passed to this onTouchEvent, if false is returned, the subsequent MontionEvent will be passed to the upper-layer onTouchEvent, for example, the onTouchEvent of Layout ),

For View dispatchTouchEvent, the source code indicates that onTouchListener is called first. If it returns true, onTouchEvent is not called, and dispatchTouchEvent returns true directly. If it returns false, onTouchEvent is called, and the return value of onTouchEvent is the final return value,

Note: The dispatch function of ViewviewGroup is called directly by the system upper layer, while other ontouchevents and onTouchListener are called by dispatch. the return value is also returned to dispatch first, and then to dispatch, therefore, the core should be in the dispatch function, so it is best not to rewrite the function directly,

In summary, I personally think that dispatchTouchEvent and onTouchEvent should not be directly overwritten, and super code should be called in a timely manner;


This article is from the "Essays in wenjian minor" blog. For more information, contact the author!

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.