Android-event trigger mechanism

Source: Internet
Author: User

Summary: The returned values here have a concept of "consumption. if the put event (down, move, up) is imagined as a commodity, it is easy to understand. true indicates that I have consumed the service. (If I want this product, you can't, so I will not pass it back ). false indicates that I did not consume the product (I don't want this product, I will pass it back to you, you love it or not ).
The order in which MotionEvent events are transmitted in onInterceptTouchEvent () and onTouchEvent ()

OnInterceptTouchEvent () is used to process events and change the event transmission direction. You can write code in the function to process events. The returned value determines the transfer direction. If the returned value is false, the event will be passed to the onInterceptTouchEvent () of the subcontrol. If the returned value is true, the event will be passed to the onTouchEvent () of the current control (), instead of being passed to the sub-controller, this is the so-called Intercept (truncation ).

OnTouchEvent () is used to process events. The returned value determines whether the current control consumes the event (consume. Maybe you want to ask whether the consumption is different. I have compiled the processing code for the event? The answer is yes! For example, the premise that ACTION_MOVE or ACTION_UP occurs is that ACTION_DOWN has occurred. If you have not consumed ACTION_DOWN, the system will think that ACTION_DOWN has not happened, so ACTION_MOVE or ACTION_UP cannot be captured.

Source Address of this article: http://www.cnblogs.com/rocky_yi/archive/2011/01/21/1941522.html#, reprint please indicate the source!
<? Xml version = "1.0" encoding = "UTF-8"?>
<Com. touchstudy. LayoutView1 xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Com. touchstudy. LayoutView2
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center">
<Com. touchstudy. MyTextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/TV"
Android: text = "AB"
Android: textSize = "40sp"
Android: textStyle = "bold"
Android: background = "# FFFFFF"
Android: textColor = "# 0000FF"/>
</Com. touchstudy. LayoutView2>
</Com. touchstudy. LayoutView1>

When onInterceptTouchEvent () and onTouchEvent () are not overwritten (their return values are both false), for the above layout, the transmission order of MotionEvent events is as follows:

When the onInterceptTouchEvent () value of a control is true, a truncation occurs and the event is uploaded to the onTouchEvent () of the current control (). For example, if we return the value of onInterceptTouchEvent () of LayoutView2 to true, the transfer process is changed:

If we set the onInterceptTouchEvent () and onTouchEvent () of LayoutView2 to true at the same time, LayoutView2 will consume the passed events and subsequent events (such as ACTION_MOVE or ACTION_UP following ACTION_DOWN) it is directly sent to the onTouchEvent () of LayoutView2 and not to any function of any other control. At the same time, an ACTION_CANCEL event is passed to the sub-space. The transfer process is changed to (the ACTION_CANCEL event is not shown in the figure ):

Sample corresponding Java code (from http://blog.csdn.net/ddna/archive/2010/04/11/5473293.aspx)
OnInterceptTouchEvent and onTouchEvent Call Sequence
Category: Android Development Comprehensive 14949 people read comments (28) collect reports

OnInterceptTouchEvent and onTouchEvent Call Sequence

OnInterceptTouchEvent () is a method of ViewGroup. The purpose is to intercept related events before the system triggers onTouchEvent () to the ViewGroup and its various childView. The idea of Android design is also very understandable, because ViewGroup contains several childView S, you need to be able to monitor various touch events in a unified manner. Therefore, this method is not available for controls that cannot contain child views, such as LinearLayout, textView does not exist.

OnInterceptTouchEvent () is also very easy to use. If you overwrite this method in ViewGroup, You can intercept various touch events. However, it is complicated to intercept all touch events. The transfer mechanism of touch events in onInterceptTouchEvent () and onTouchEvent and between various childView depend entirely on onInterceptTouchEvent () and onTouchEvent. In addition, the returned values for down event processing directly affect the receipt and transmission of subsequent move and up events.

The basic rule for the return value is clear. If return is true, the method consumes the event. If return is false, the method is not completely processed, the event still needs to be passed in some way and continues waiting for processing.

SDK explanation:
Public boolean onInterceptTouchEvent (MotionEvent ev)
Since: API Level 1

Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.

Using this function takes some care, as it has a fairly complicated interaction with View. onTouchEvent (MotionEvent), and using it requires implementing that method as well as this one in the correct way. events will be removed ed in the following order:
You will receive the down event here.
The down event will be handled either by a child of this view group, or given to your own onTouchEvent () method to handle; this means you shoshould implement onTouchEvent () to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it ). also, by returning true from onTouchEvent (), you will not receive any following events in onInterceptTouchEvent () and all touch processing must happen in onTouchEvent () like normal.
For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent ().
If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent () method and no longer appear here.
Parametersev The motion event being dispatched down the hierarchy.

Returns
Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent (). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

The onInterceptTouchEvent () mechanism is complex and the description above is also complex. To sum up, the basic rules are as follows:

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

2. if the onInterceptTouchEvent () of the ViewGroup returns false after the down event is processed, the subsequent move, up, and other events will be passed to the ViewGroup first, the onTouchEvent () process is passed to the final target view just like the down event.

3. if the onInterceptTouchEvent () of the ViewGroup returns true after it receives the down event processing, the subsequent move, up and other events will not be passed to onInterceptTouchEvent (), the onTouchEvent () that is passed to the ViewGroup like the down event. Note that the target view will not receive any event.

4. If the onTouchEvent () of the view that finally needs to process the event returns false, the event will be passed to the onTouchEvent () of the view at the previous level for processing.

5. If the onTouchEvent () of the view that finally needs to process the event returns true, subsequent events can be passed to the onTouchEvent () of the view for processing.

 

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.