Android-onintercepttouchevent () and ontouchevent () summary

Source: Internet
Author: User
Tags truncated

Frankly speaking, these two small east is too troublesome, very difficult to understand, my own that API documents are dizzy, find a lot of information on the Internet, just know is how to, here summarize, remember this principle will be very clear:

1, Onintercepttouchevent () is used to handle events (similar to preprocessing, of course, can not handle) and change the direction of the event, that is, to allow the touch event to continue down (child control) delivery, One but returns True (representing the event being processed in the current viewgroup), the path down passes is truncated (all child controls will not have the opportunity to participate in the touch event), and the event is passed to the current control's ontouchevent () processing; returns false. The event is given to the child control's onintercepttouchevent ()

2, Ontouchevent () is used to handle the event, and the return value determines whether the current control consumes (consume) the event, that is, whether the current control also allows the touch event to continue up (the parent control) after the touch event has been processed, but returns true, The parent control does not have to worry about dealing with the touch event. Returns true, which is passed up to the parent control (note: May you think it's a matter of spending, anyway I've written the code for the event?) The answer is a difference! For example, action_move or action_up occurrence of the premise is must have occurred action_down, if you do not consume Action_down, then the system will think Action_down did not happen, so Action_ Move or action_up cannot be captured. )

Concept Introduction

1, Onintercepttouchevent () is used to handle the event (Focus Onintercepttouchevent The event is passed from the parent control to the child control until it is blocked or to a view without the event, and then back from the child to the parent control, This time is Ontouch) (similar to preprocessing, of course, it can be handled) and change the direction of the event, that is, whether to allow the touch event to continue down (the child control) pass, but return True (representing the event in the current viewgroup will be processed), The path down passes is truncated (all child controls will have no chance to participate in the touch event), and the event is passed to the current control's ontouchevent () processing; returns false, giving the event to the child control's onintercepttouchevent ()

2, Ontouchevent () to handle the event (Focus Ontouch The event is passed back from the child control to the parent control, a layer down), the return value determines whether the current control consumes (consume) this event, that is, after the current control has finished processing the touch event, Whether the touch event is also allowed to continue to pass up (the parent control). Returns false, which is passed up to the parent control, in detail the touch event is given to the parent control, then the up event is followed by the touch trigger, not the child controls passed to it. If the parent control is still false, the touch's processing is given to the parent control's parent control, and the up event handling is in the parent control's parent control and does not trigger the following.

Returns true if the child control returns True, then its touch event is handled here, and the parent control cannot handle it because it does not receive the touch that the child control passes to him, and the quilt control is intercepted. (Here long-winded so much is to deepen the memory, this two events understand is so troublesome, not to mention to remember, remember I must be suddenly forget the ^0^)

(Note: May you think it's a matter of spending, anyway, I've already written a deal code for the event?) The answer is a difference! For example, action_move or action_up occurrence of the premise is must have occurred action_down, if you do not consume Action_down, then the system will think Action_down did not happen, so Action_ Move or action_up cannot be captured. )

Detailed Introduction

Onintercepttouchevent () is a method of ViewGroup to intercept related events before the system triggers ontouchevent () to the ViewGroup and its various childview. The idea of Android so designed is also well understood, because ViewGroup will contain several childview, so there is a need to be able to monitor the opportunities of various touch events uniformly, so the pure can not contain sub-view control is not this method, such as LinearLayout has , there is no textview.

Onintercepttouchevent () is also simple to use, and if you overwrite the method in ViewGroup, you can intercept a variety of touch events. But how to intercept, whether all touch events need to intercept is more complex, touch event in Onintercepttouchevent () The transfer mechanism between the ontouchevent and the Childview is entirely dependent on the return value of Onintercepttouchevent () and Ontouchevent (). Also, the return value for the down event processing directly affects the receipt and delivery of subsequent move and up events.

On the question of the return value, the basic rule is clear, if return true, then indicates that the method consumes this event, if return False, then indicates that the method is not processed completely, the event still needs to be passed in some way to continue waiting for processing.

Onintercepttouchevent () is a method of ViewGroup to intercept related events before the system triggers ontouchevent () to the ViewGroup and its various childview.

  1. The down event is first passed to the Onintercepttouchevent () method

  2. If the ViewGroup onintercepttouchevent () return false after receiving the down event processing, then the subsequent move, up, and so on will continue to be passed to the ViewGroup first. It is then passed to the ontouchevent () processing of the final target view like the down event.

  3. If the ViewGroup onintercepttouchevent () return true after receiving the down event processing, subsequent moves, up, and so on will no longer be passed to Onintercepttouchevent (), Instead, the ontouchevent () process passed to the viewgroup like the down event, noting that the target view will not receive any events.

  4. If the ontouchevent () of the view that eventually needs to handle the event returns false, the event is passed to the ontouchevent () processing of the view at its previous level.

  5. If the ontouchevent () of the view that eventually needs to handle the event returns TRUE, then subsequent events will continue to be passed to the view's ontouchevent () processing.

    Simply by looking at this official document explanation, it is clear that the two functional relationships and uses are definitely experienced framework gurus.
    Otherwise, there must be a case to illustrate. Suppose we have such a layout, very typical of

    1. <com.test.layoutview1 xmlns:android="Http://schemas.android.com/apk/res/android"
    2. android:orientation="vertical" android:layout_width="Fill_parent"
    3. android:layout_height="Fill_parent">
    4. <com.test.layoutview2
    5. android:orientation="vertical" android:layout_width="Fill_parent"
    6. android:layout_height="Fill_parent" android:gravity="center">
    7. <com.test.mytextview
    8. android:layout_width="Wrap_content" android:layout_height="Wrap_content"
    9. />
    10. </com.test.LayoutView2>
    11. </com.test.LayoutView1>

    Use an example diagram to explain this layout:


    Usually the periphery of the layoutview1,layoutview2, just the layout of the container does not need to respond to touch screen Click events, just mytextview need to click accordingly. But this is only the general case, some special layout may also need to respond to the peripheral container, even do not let the inside of the Mytextview to respond. In a more special case, the response object is dynamically replaced.
    So first look at the pass-through process between the two functions of the default touch-screen event. Such as:

    If you just want Mytextview to respond to touch-screen events and let Mytextview's ontouchevent return true, then the event stream becomes like, you can see LAYOUTVIEW1, Layoutview2 has been unable to enter Ontouchevent:

    Another case is that the peripheral container wants to handle the touchscreen event on its own, so it should return true in the corresponding onintercepttouchevent function to intercept the touch-screen event, such as Layoutview1 for interception, and the processing stream to become such as:

    And so on, we can get a variety of specific situations, the entire layout of the view class level have the opportunity to intercept, and can see the outside of the container view has preferential interception rights.

    When we are doing something that is relatively more complex with touch-screen interactions, it is often necessary to dynamically change the processing objects of the touch event, such as the launcher standby desktop and the main menu (see), from the start of the slide screen to the stop-swipe process, Only the peripheral container view can handle touch event, or it will mistakenly click on the app icon or widget above. Conversely, in a stationary state, you need a touch event that responds to the icon (child view). The Abslistview code in the extract framework is as follows

    1. Public Boolean onintercepttouchevent(motionevent ev) {
    2. int Action = ev. Getaction();
    3. Switch (action & motionevent. Action_mask) {
    4. case motionevent. Action_down: {
    5. if (touchmode = = touch_mode_fling) {
    6. return true; //fling status, intercept touch, because in sliding state, do not let child view handle
    7. }
    8. break;
    9. }
    10. case motionevent. Action_move: {
    11. Switch (mtouchmode) {
    12. case Touch_mode_down:
    13. final int pointerindex = ev. Findpointerindex(mactivepointerid);
    14. final int y = (int) ev. GetY(pointerindex);
    15. if (startscrollifneeded(y - mmotiony)) {
    16. return true; Start sliding state, intercept touch events, don't let child view handle
    17. }
    18. break;
    19. }
    20. break;
    21. }
    22. }

    Summarize:

    It is difficult to understand the purpose of the Onintercepttouchevent function simply through an overview of the official documents, only by deducing this abstract rule, with graphics and text, to acquire this important knowledge. Obviously, the default is to return false, not to intercept. When True, the back-end control of the event stream has no chance to handle the touch event, treating each handler function in the default event stream as a node, as long as the node returns TRUE, and subsequent events are cut off, which is a good understanding.

    Onintercepttouchevent is defined in the ViewGroup. The layout classes in Android are generally inherited from this class. Onintercepttouchevent is used to intercept gesture events, and each gesture event calls Onintercepttouchevent first.
    Onintercepttouchevent () is used to handle events and change the direction in which events are passed. The return value is False when the event is passed to the child control's onintercepttouchevent (), and the return value is true when the event is passed to the current control's ontouchevent () instead of being passed to the child control, which is called a intercept (truncation).
    Ontouchevent () is used to handle the event, and the return value determines whether the current control consumes (consume) the event. Maybe you want to ask if there is a difference between consumption, anyway, I have written the processing code for the event? The answer is a difference! For example, action_move or action_up occurrence of the premise is must have occurred action_down, if you do not consume Action_down, then the system will think Action_down did not happen, so Action_ Move or action_up cannot be captured.

The onintercepttouchevent default value in ViewGroup is false in order to pass events to the ontouchevent in view.

The ontouchevent default value in ViewGroup is false.

The ontouchevent in view returns the default value of True. This allows you to perform multiple touch events.

(EXT) android-onintercepttouchevent () and ontouchevent () summary

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.