Android Curiosity Baby _08_ Chat Touch Event Delivery

Source: Internet
Author: User

Explain the principle of touch event transmission on the internet there are a lot, from the source point of view, from the perspective of practical examples. I am here only to record their own understanding, said may not have other Daniel thorough, has the wrong kneeling beg to comment.


Direct, control the transmission of touch events with illustrations and text.


(1) Messy graphic version:


(2) Look at the words:


(2.1) does not contain activity,activity there is no Onintercepttouchevent method, and the default implementation is passed directly down.


(2.2) explains the meaning of the 3 touch event control method and the meaning of its return value:


<1>onintercepttouchevent

Onintercepttouchevent is a unique method of viewgroup, activity and view do not have this method, it is used to intercept touch events are no longer passed to the child view (activity and view and no child view, So there is no logical way to do this, and the default implementation does not intercept. If you intercept, the child view will completely accept no touch events, and the method of any related touch events in the child view will not be called. (Note: There are exceptions I will say later)(with special circumstances)

Returns true:
Represents the interception of touch events and does not continue to invoke the Dispatchtouchevent method of the child view, calling its own Ontouchevent method directly. (with special circumstances)

return false:
Indicates that the event is not intercepted and continues to invoke the dispatchtouchevent of the child view.


<2>dispatchtouchevent

For assigning touch events, note that assigning events, rather than handling events, should be done by determining the direction in which events are passed, rather than dealing with consuming touch events.

Returns true:

Say that they want to consume the event, but not to consume it immediately, but to consume it if no one is spending it on their child view. The event will continue to pass, but if you can reach yourself in the process, you will consume the event.

return false:

Indicates that he does not consume this event, and if Ontouchevent also returns false indicating that the event is not consumed, it will continue to be passed on to the parent's ontouchevent.


<3>ontouchevent

For handling events, if you want to handle touch events and make corresponding actions, you should do so in this method.

Returns true:

said that they want to consume the event, and immediately take effect, immediate consumption, event delivery stopped.

return false:

Say that you do not want to consume this event, while checking dispatchtouchevent before returning true, you will find: oops, I said before the child view no one to deal with, then I will deal with. Well, I'll keep my word, and I'll take care of it, and don't pass it on.


Note the difference between dispatchtouchevent and Ontouchevent returns true:


Dispatchtouchevent is no one to spend the words you give me to spend, you are no one to Ah, then give me, you are really good ah.


Ontouchevent is I want to consume now, other people are not share, why do go, is so domineering, is so capricious.


(2.3) What may happen in the course of transmission

the first recipient of a touch event is always activity, activity typically does not consume touch events, but is passed directly to the collection of view trees in the scope of the touch event. The level position in the view tree corresponds to the position in the touch event, that is, the outer view is located on the top level, and the child is on the bottom. That is, the activity is at the top, then the container control, and the last view at the bottom.


Under normal circumstances:

Events go from top to bottom, through every dispatchtouchevent. When you reach the last view, call its ontouchevent, start the transfer process from the bottom up, passing through each of the ontouchevent, which is the case where no view wants to consume touch events.


A few turning points may occur:

<1> in the process from the top down, if a view's onintercepttouchevent returns True, indicating that the event will not continue to pass down, but instead calls the view's ontouchevent directly, Note that this does not mean that the view consumption of this event, but to intercept the event continues to pass, depriving the child view of the consumption rights, early start from the bottom of the process, whether it consumption also depends on its ontouchevent return value is True . (with special circumstances)


<2> In the bottom-up process, if a view's ontouchevent returns True, indicating that it wants to consume the event, the event will not continue to pass up, and the event pass ends.


<3> or in the process from the bottom up, if a view in the previous Dispatchtouchevent method returns True, indicating that the previous view indicates that if all child view does not consume this event, then let me consume, the event will not continue to pass on, Event delivery ends.
(All sub-view does not consume this event means that all sub-view dispatchtouchevent and Ontouchevent return false, otherwise the event is consumed and passed over, and will never reach itself again.) )


<4>
This is less, and a child view calls the parent's Requestdisallowintercepttouchevent method, which means that the parent is requested not to intercept the event. In this case, the parent's onintercepttouchevent returns true and cannot intercept the event, that is, to invalidate the Onintercepttouchevent method, which is the above 3 tokens (with special circumstances) The special circumstances mentioned.


(2.4) Memory function

Memory function means that if a view consumes a down event, then the next move and up events are passed down from the top of the activity, but when passed to the view, even if the view is not used Onintercepttouchevent method to intercept an event, the event will still be intercepted and processed directly by the view. That is, if a view consumes a down event, then the next move and up event will be given to it for consumption.

The reason for this is that a single touch event does not form an effective action in the general case. For example, a drag action requires a down event and a continuous move event, and a click action requires at least one down event and up event. Assigning a single touch event to a different view often does not create meaningful actions, so adding this memory function reduces the transmission of events.


(3) Just say something.

Understanding the delivery of touch events is generally done to address a situation where touch events conflict, and one thing to be aware of when dealing with touch event collisions:

We have to deal with who will handle the touch event, instead of modifying the consumer's view of the event to be handled.

It's a bit of a mouthful, isn't it, again white is that we should control who to deal with, rather than control how to deal with, each view has its own processing logic, we should not modify it.
Give me a chestnut:
Finger left and right can not drag Viewpager, we think should be how to drag the finger around the situation so that viewpager can get the consumption of touch events, and do not accept the touch event after how to let viewpager with the finger scrolling, This feature Viewpager itself has been implemented, you put the touch event to it, it can roll.


Well, this concludes, it is recommended that the touch event delivery mechanism unfamiliar to write an example of their own to print some log logs to verify, and then Baidu Google next a common touch conflict and its solution.


Ask for praise and seek advice!!!

Android Curiosity Baby _08_ Chat Touch Event Delivery

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.