The distribution process for Android touch events

Source: Internet
Author: User

I do not know if you are involved in the Android touch screen event when there are the following questions:

1.View the Ontouchevent () method returns True and false what is the difference? The explanation given by the SDK is simple: "Return true indicates that the event has been processed, return false to the contrary", this sentence does not explain clearly the problem.

The 2.View ontouchevent () method returns True when processing the Action_down, returns false when processing the Action_move, indicates whether it is handled or not processed? Return Super.ontouchevent () What does it mean?

3. Rewrite the Ontouchevent () method and set a touch screen listener through Setontouchlistener () What the difference is, it looks like it's very similar.

4.View dispatchtouchevent (), Ontouchevent (), Setonclicklistener (), ViewGroup's onintercepttouchevent () I was dizzy, How do you use these methods to rewrite?

5. Suppose that a viewgroup has two sub-views, the two view is partially overlapping, click on the overlapping section, which view will be processed by the event?

6. The most important question is: How does the touchscreen event pass from the top ViewGroup down?

If you have a similar question, believe that my blog can give you an answer.

Two. The first definite points to be made are:

1.View is typically used to display certain content, and it is also usually designed to handle interactive events such as a user's touchscreen, while ViewGroup is present as a container for view, although it is a subclass of view in code, but it is usually just a sub-view layout that the container uses to organize it.

2. We know that the Android view level is a tree structure, it needs to be clear that a viewgroup its direct sub-view of the tree structure of the son, and then down a layer of not forget, similar to the inter-process parent-child relationship. For example, Framelayout has two sub-views, namely LinearLayout and TextView, and LinearLayout has three sub-views ImageView, then Framelayout Getchildcount () is called. Method will only return 2, not 5. So the term "child view" in the following represents a direct sub-view of a viewgroup, which could be a view class or a ViewGroup class.

The topmost view of the 3.Activity view is Decorview, which is generated in the Phonewindow class through the Generatedecor () method, which inherits from Framelayout and is the root view of the view hierarchy.

4. There are three main events for touch screen: down,move,up

So how exactly does a touchscreen event pass down the view level? (This is only considered when the event has reached Decorview, in fact, the Viewrootimpl class received the Inputdispatch passed over the event, this is not written here), Viewrootimpl in Deliverpointerevent ( ) method by calling Mview.dispatchpointerevent (event), passing the touchscreen event to Decorview,decorview through Dispatchtouchevent () and continuing down to the child view. If the child view is also a viewgroup, it calls its own dispatchtouchevent () method to pass down, and if the child view is a view, then the Ontouchevent () method of the child view is called, and if the child view processes the event, Then the event delivery is aborted. The whole process is like a recursive process, understanding how a ViewGroup is passed through Dispatchtouchevent () to its sub-view level and understands the whole process.

Here does not analyze the code of the ViewGroup Dispatchtouchevent () method, directly give me the conclusion, interested readers can analyze to see.

Three. Summary

The following scenario assumes that a ViewGroup has three sub-views, which are v1,v2,v3 in index order. V1 is also a viewgroup,v2 and V3 are common view, and they have a little overlap of the part.

1.ViewGroup Dispatchtouchevent () distributes the event down to its child view, then it is distributed to the Ontouchevent method that V3 calls it, and if V3 does not handle the event, it will continue to be distributed to V2, if V2 does not handle the event, will continue to be distributed to V1, because V1 is a viewgroup, it is called its dispatchtouchevent () distribution to its child view.

2.v3 does not handle this event means: When the down event arrives, the Ontouchevent () method returns false and if True is returned when the down event is received, the event is processed. It doesn't matter what you return when you receive the move and up events. So the idea is that as long as you're willing to deal with the down event, you have to deal with the next other events.

3.v3 can receive touch-screen events on the premise that its display rectangle must be within the range of the touchscreen, the obvious reason, otherwise the event will be passed to v2.

4. If v1,v2,v3 decides not to handle the touchscreen event, then the event is eventually handled by ViewGroup itself, and its ontouchevent () method is called.

5. If the event is passed to v1,v1 whether processing depends on its child view, if its child view has one that handles the event, then the event is handled on behalf of V1, if all its child views do not handle the event and V1 itself ontouchevent () method returns False when handling the down event, which means that V1 does not handle the event.

6. If a valid listener is set through Setontouchlistener (), then the event arrives and the listener is called directly without calling Ontouchevent () and returns True, indicating that the event has been processed.

Here, the meaning of the ontouchevent () return value should be very clear, so what is the Super.ontouchevent () return value? It's easier to see the code, if a view is not clickable or not longclickable, Then Super.ontouchevent () returns false directly, otherwise the onclick and Onlongclick processing and returns TRUE. You can call Setclickable (true), setlongclickable (true) to change the state of the view, and the same effect is called Setonclicklistener () and Setonlongclicklistener ().

By the above conclusion, if the two views are the parent relationship, and they overlap with each other, click on the overlapping section, the first to handle the event is a larger subscript than the view, if this view does not want to handle the event, let the other processing.

Four. Onintercepttouchevent ()

ViewGroup can call its onintercepttouchevent () method to intercept the child view of the event, this method by default returns false to indicate not to intercept if the onintercepttouchevent () True is returned when the down event is received, then the next down,move and up events are received by the ViewGroup own Ontouchevent () method, all the child views are not receiving events, and ViewGroup own The Onintercepttouchevent () method also has only the down event passed over, because it is handled by the parent view, so it is meaningless to receive the move and up events, so only the down event is passed. Note: There is no touch targets and this action is a initial DOWN,SO this view group continues to intercept touches .

If a child view decides to handle all three events, the Onintercepttouchevent () method of the parent view is called first when each event arrives, and if True is returned on an event, Then the event is intercepted and subsequent events are processed to ViewGroup's own ontouchevent () method, and the child view receives the Action_cancel event.

Example: If the sub-view handles the down event, but ViewGroup intercepts the move event when the move arrives, the child view will not receive the next move and up events, and receives the Action_cancel event. The ViewGroup will receive the move and up events, and the Onintercepttouchevent () method will only receive the down and move events.

Interception methods are sometimes very useful, for example ScrollView it will first put the down event to the child view processing, if it is a click event, to the child view, if it is determined to be dragging the child view, then will intercept the move event, by itself processing, call Overscrollby () produces scrolling.

Of course, if you rewrite the ViewGroup dispatchtouchevent () method to control the distribution of events, and the above process is not necessarily the same.

Summed up, I believe that all the questions at the beginning have an answer, a bit around people, the understanding of the clear.

Reprinted from: http://www.bdqn.cn/news/201312/12158.shtml

The distribution process for Android touch events

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.