Android processes click events and android click events

Source: Internet
Author: User

Android processes click events and android click events
Two days ago, I want to learn how to handle Android click events. I have translated a foreign file, which is attached to the original English version. How does Android handle click events?

Android click events are encapsulated in motionevents. Click events can be divided into the following types:

-ACTION_DOWN-ACTION_UP-ACTION_MOVE-ACTION_POINTER_DOWN-ACTION_POINTER_UP-ACTION_CANCEL click Event data includes:-Touch loaction-Number of pointers (fingers)-Event time a click Event starts from ACTION_DOWN, ended In ACTION_UP. The first response to the click event is the dispatchTouchEvent () callback in the Activity, which is passed in sequence from the parent View to the Child view. During the transfer process, click events can be intercepted at any time. Click the event to pass down from top to bottom and return in reverse direction until the click event is processed. In the View, the ACTION_DOWN action must be processed to respond to the click event. To improve efficiency, once the click event is processed, it will not continue to pass down. Any unprocessed click events will eventually return to the onTouchEvent () callback function in the Activity, and end here. You can use an optional external OnTouchListener in any View/ViewGroup to intercept click events. Activity. dispatchTouchEvent (): This callback is usually called first and sends a click event to the root view associated with the current user interface. -If onTouchEvent () does not have a view to process click events, this callback will be called, usually called at the end. View. dispatchTouchEvent (): This callback first sends a click event to the listener (listener). If a listener exists, the callback View is called. onTouchListener. onTouch (); If the click event is not processed, the View is called back. onTouchEvent () to process click events. ViewGroup. dispatchTouchEvent ():-onInterceptTouchEvent () functions of this function: -Check whether the Sub-view needs to be replaced-Send the ACTION_CANCEL event to the sub-view of the activity-return the true value once all the events in the sub-sequence are processed-reverse each sub-view according to the added order- if the click event is related (in the view ), call child. dispatchTouchEvent ()-if the click event is not processed by the previous view, it is distributed to the next view. -If no sub-view is available to process listener events, the listener receives the opportunity to process click events through the following callback: OnTouchListener. onTouch ()-if there is no listener or the click event is not processed, the following function is called: The onTouchEvent () Intercepted click event will stop being passed to the subview. Common click event handling methodsThe onTouchEvent () method can be rewritten by subclass to handle click events and the OnTouchListener listener can be provided. Destroy Click Event-Return true with ACTION_DOWN to show interest returns true even if you are not interested in ACTION_DOWN. -For other events, returning true will simply stop further processing. Useful constants available in ViewConfiguration-getScaledTouchSlop () Distance move events might vary before they shocould be considered a drag displacement event may have variable-getScaledMinimumFlingVelocity () used to get the click movement speed. The system determines whether to drag slowly or quickly-getLongPressTimeout () the system determines whether a long-click event is obtained based on the data obtained. The system displays different values based on the resolution of each device and forwards the click event. The system calls the dispatchTouchEvent () in the target view () -Avoid calling onTouchEvent () of the target view to steal the click event.-Override onInterceptTouchEvent () in the subclass () -If you want to take over the Click Event Processing Event, return true. All the subsequence click events for the current action will go directly to the onTouchEvent () callback, and onInterceptTouchEvent () will no longer be called for each event () callback Function-any current target view will receive ACTION_CANCEL User click event handling warningCall the upper-layer processing method-View whenever needed. onTouchEvent () implements a lot of state management (pressed, checked, etc.) For click events .), if you capture each click event, you will miss a lot of content. ACTION_MOVE protection through the slop check-prevents the view containers with excessive contact area or sliding fingers from handling the ACTION_CANCEL event-view containers with user operation actions (such as scrolling) Steal click events, you need to reset the status. -Remember that no other messages will be received after the CANCEL operation. Do not intercept click events unless you are about to process them-intercepting click events is irreversible. Multi-Click Event ProcessingMotionEvent. getPointerCount ()-Get the number of contacts on the current screen. Use the ACTION_POINTER_DOWN and ACTION_POINTER_UP events to detect the second contact-MotionEvent. getActionMasked ()-MotionEvent. getActionIndex () use the MotionEvent method to obtain specific contact data from contact list parameters-if a parameter is not passed in the method, the data of the first contact is usually returned. Batch ProcessingTo improve efficiency, ACTION_MOVE events can process the latest click events in batches within a MotionEvent. Generally, the corresponding data can be obtained through the following standard methods:-getX (), getY (), getEventTime () occurs between the current and previous ACTION_MOVE click events can be obtained through the following method to obtain historical data:-getHistoricalX (), getHistoricalY (), getHistoricalEventTime ()-getHistoricalSize () method return the number of click events for batch processing. When all click events have the maximum time precision, you can reconstruct all click events. System click HandlerIf not, you do not need to jump to the custom click handler immediately. OnClickListenerOnLongClickListenerOnTouchListenerOnTouchListener-monitor the MotionEvent of individual classes without subclasses-you can process click events in the listener-you can clear the View processor OnScrollListener/View in advance. onScrollChanged ()-process a view with the scroll function when a rolling event occurs. process more complex click interaction gesture detector-onDown (), onSingleTapUp (), onDoubleTap ()-onLongPress () -onScroll () (drag by user using mobile phone)-onFling () (drag by user quickly end) Balance gesture detector-onScaleBegin (), onScale (), onScaleEnd () via OnTouchListener () or onTouchEvent () Processing http://files.cnblogs.com/files/cn-bloger/PRE_andevcon_mastering-the-android-touch-system.pdf

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.