DispatchTouchEvent, onTouchEvent and onInterceptTouchEvent

Source: Internet
Author: User

1. First of all, I understand the common sense that the View does not have an onInterceptTouchEvent event. However, ViewGroup has all three events. After the viewgroup inherits the View, a method called onIntercepTouchEvent is added.
It can be seen from the literal meaning that onInterceptTouchEvent is used to intercept events, dispatchTouchEvent is used to distribute events, and onTouchEvent is used to process events.
It is easy to see that we should first dispatchTouchEvent and then onTouchEvent. When Will OnInterceptTouchEven be called? In order to better understand these three events, we should first consider the Sub-view, a viewgroup, and then a sub-view in the viewgroup.

2. For a View, the event is the dispatchTouchEvent of the View first, and then the onTouchEvent (or not ).

When will it not go through onTouchEvent? When the dispatchTouchEvent is rewritten, the system returns false without going to super. dispatchTouchEvent, so that it does not go through the onTouchEvent.
Of course, this is against the common sense of the android architecture. General dispatchTouchEvent is not recommended to be rewritten. However, we can conclude this conclusion through this case.

When the event arrives at the view, it first dispatchTouchEvent. In the system's dispatchTouchEvent, it will call the Ontouch method of the view. If the onTouch method returns true in the down event

The dispatchTouchEvent method also returns true, and all the subsequent move and up events are passed to onTouch. After the move event and the return value of the up event, onTouch returns the dispatchTouchEvent and the return value.

On the contrary, if the first down event is sent to ontouch, ontouch returns false, and the event will not be transmitted, that is, it will not go through dispatchTouchEvent. It will not go through ontouchevent
3. For a ViewGroup (when there is no sub-view ):

The event trend is dispatchTouchEvent-> onInterceptTouchEvent-> onTouchEvent

We will find that their logic is no different from the view logic, but the onInterceptTouchEvent will be in the middle when the down event is taken, and here no matter what onInterceptTouchEvent returns, it will not interfere with it like 2. description

Is the onInterceptTouchEvent method useless?

4. When the Viewgroup contains a subview


Down event direction: viewgroup. dispatchTouchEvent-> viewgroup. onInterceptTouchEvent-> if true is returned-> viewgroup. onTouch --------------------------- branch 1
|-> If false is returned-> view. dispatchTouchEvent branch 2



Branch 1: The move or up event follows the following trend: viewgroup. dispatchTouchEvent-> viewgroup. ontouch. Here, this trend is returned no matter what ontouch returns.

Branch 2: The down event arrives at view. dispatchTouchEvent-> view. onTouch-> return true-> branch 3
|-> Return false-> viewgroup. ontouch-> return true-> move, up and other events viewgroup. dispatchTouchEvent-
|-> If false is returned, the viewgroup will no longer receive subsequent events.
> Viewgroup. ontouch


Branch 3: The onTOuch of the sub-view returns true, indicating that the sub-view can accept the event. The future event direction is

Move: viewgroup. dispatchTouchEvent-> viewgroup. onInterceptTouchEvent?

If the returned value is false, the subsequent move and up operations will follow viewgroup. dispatchTouchEvent-> viewgroup. onInterceptTouchEvent-> view. dispatchTouchEvent-> view. ontouch

If true is returned, the next step of the move event of the sub-view is to force the Cancel event and the UP event to the view and view. dispatchTouchEvent-> view. ontouch (ignore what it returns)-> then leave the Move event to viewgroup: viewgroup. dispatchTouchEvent-> viewgroup. ontouch

 

This phenomenon should have been seen in listview or scrollview. When a user presses a button in scrollview, the user finds that the button has responded accordingly (the button is highlighted ), but when you press and hold it, you will find that the interface is rolling. This is because the onInterceptTouchEvent is an event!

If I have time, I will make an app to verify the above. Thank you!

 

Go to: http://www.eoeandroid.com/thread-262615-1-1.html

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.