Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use View to realize event Distribution in Android

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "how to use View to achieve event distribution in Android", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use View to achieve event distribution in Android" can help you solve your doubts.

Three methods

Public boolean dispatchTouchEvent (MotionEvent ev)

For the distribution of events, the return result is affected by the following two methods, indicating whether the event is consumed.

Public boolean onInterceptTouchEvent (MotionEvent ev)

Whether the event is intercepted. Return true: intercept; false: not intercept.

Public boolean onTouchEvent (MotionEvent ev)

Indicates whether the event is handled

3. The relationship between the three methods

Public boolean dispatchTouchEvent (MotionEvent ev) {boolean result = false; if (onInterceptTouchEvent (ev)) {result = onTouchEvent (ev);} else {result = child.dispatchTouchEvent (ev);} return result;}

4. Principle exposition

1. The same event sequence refers to a series of events from the time the finger touches the screen to the moment it leaves the screen, in which it starts with down, generates several move events in the middle, and ends with up events.

two。 The same sequence of events can only be intercepted by one view. If it does not consume down events, then other events will not be handed over to it, and once it handles down events, future events will not call onInterceptTouchEvent this method to determine whether to intercept or not, because they will be handed over to it, so there is no need to ask.

3. If view does not consume events other than down, the click event will disappear, the onTouchEvent of the parent element will not be called, and the current view can continue to receive subsequent events, and eventually these missing click events will be handled by activity.

4.ViewGrop does not intercept any events by default.

5.VIew does not have an onInterceptTouchEvent method, and once a click event is passed to it, its onTouchEvent method is called.

When 6.View is unclickable (clickable and longClickable are both false), onTouchEvent does not consume events. In addition, onTouchEvent returns true as a consumption event by default.

The enable property of 7.View does not affect the default return value of onTouchEvent

8. Events are passed from the outside to the inside, that is, events are passed to the parent element and then distributed to the child element. Interferes with the event distribution process of the parent element through the requestDisallowInterceptTouchEvent method, with the exception of the ACTION_DOWN event.

Click event priority in 9.view: high-low: onTouchListener--- > onTouchEvent--- > onClickListener. When the onTouch method in onTouchListener returns false, onTouchEvent will call

5. Individual source code analysis

This is a piece of source code for intercepting events in ViewGroup's dispatchTouchEvent, which mainly describes the ideas of the intercepting process.

1.mFirstTouchTarget means that an event is assigned when it is successfully processed by a child element of ViewGroup, so once ViewGroup intercepts the event, mFirstTouchTarget = = null, then true is returned.

2FLAG_DISALLOW_INTERCEPT this flag bit is set by the requestDisallowInterceptTouchEvent method, which is generally located in the sub-view. Once this flag bit is set, the onInterceptTouchEvent method in VIewGroup will not be executed, and methods other than ACTION_DOWN will not be intercepted, which confirms discussion 2.

/ / Check for interception. Final boolean intercepted; if (actionMasked = = MotionEvent.ACTION_DOWN | | mFirstTouchTarget! = null) {final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT)! = 0; if (! disallowIntercept) {intercepted = onInterceptTouchEvent (ev); ev.setAction (action); / / restore action in case it was changed} else {intercepted = false }} else {/ / There are no touch targets and this action is not an initial down / / so this view group continues to intercept touches. Intercepted = true;}. / / Check for cancelation. Final boolean canceled = resetCancelNextUpFlag (this) | | actionMasked = = MotionEvent.ACTION_CANCEL;. This article "how to use View to distribute events in Android" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report