Android View Framework Summary (vii) View event distribution mechanism

Source: Internet
Author: User

Please respect the results of the sharing, reproduced please indicate the source:
http://blog.csdn.net/hejjunlin/article/details/52282833

View layout is over, from the beginning of this article view event correlation analysis, today is analyzing the view of the event distribution mechanism (PS: The source code in this article is Android 6.0, please know)

    • Distribution mechanism for View events
    • Dispatchtouchevent
    • Onintercepttouchevent
    • Ontouchevent
    • Case

Events are usually important in the following three ways:
Motionevent.action_down Press View, is the beginning of all events
Motionevent.action_move Sliding Events
Motionevent.action_up corresponds to down, indicating lift
How the event responds:
In the Android system, the most widely used is the monitoring, callback, and then formed the process of incident response. Popular means, you go out on vacation, book a hotel in advance and register, you go there, you can enter the formalities, do not have to re-register. Just enjoy your room right away.
First of all, we need to be familiar with Dispatchtouchevent and ontouchevent two methods, words not much to say, read the fucking source

View.java-Dispatchtouchevent ()


View.java-Ontouchevent ()





The above code complements the point: a clickable or longclickable view is always consuming touch events, and the general view is neither clickable nor longclickable (that is, does not consume touch events, Only executes Action_down and does not perform action_move and action_up) the button is clickable and can consume touch events, but we can pass setclickable () and Setlongclickable () to set whether the view is clickable and longclickable. Of course, you can also override the view's Ontouchevent () method to control whether the touch event is consumed or not

View.java-Checkfortap ()

View.java-Checkforlongclick ()

View.java. $CheckForLongPress, Run ()

View.java. PerformClick ($CheckForLongPress)

For a small example, we know that the buttons, ImageView, TextView, and so on are usually inherited from the parent view. Relativelayout, LinearLayout, framelayout and other layouts are inherited from the parent class ViewGroup to implement, the following button to demonstrate the view event distribution, of course, I will switch TextView, To see if they are different:
Mainactivity, Eventbutton set up Ontouchlistener monitoring, and it itself has ontouchevent, and ondispatchtouchevent


This is a custom button


Layout file

Running in studio, on the phone

Click the button to see the log output:

As you can see, the execution process is
Dispatchtouchevent (), Setontouchlistener (), Ontouchevent ()

If the finger is long-pressed, exceeding the system's perceived time, then it is considered to be a long key, which can be found to be triggered at down, which corresponds to when we analyze the source code, and when up, triggers the onclick
In this said, if the above Eventbutton inherit TextView words, press the lift, there will be a special phenomenon, I can tell you the phenomenon: is only dispatchtouchevent Action_down,ontouch Action_down , Ontouchevent Action_down These three, you move, or lift, is no move, or up, this answer, I tell everyone at the beginning of the next section (the careful classmate in this article can also find the answer, hey).

From the above log can be seen, set the Onlongclicklistener is executed first, then to perform the Click event, this view must consume Action_down and Action_move events, And without setting Onlongclicklistener, if Onlongclicklistener is set, Onlongclick () is required to return false to execute to the onclick () method, and perhaps you will wonder, The general view default is not to consume touch events, this is not contrary to what you said above, we have to execute the Click event must call Setonclicklistener () to set the Onclicklistener interface, look at this callback:

In other words, if the view is not/clickable, a clickable is set to True when the transpose Setonclicklistener is set.

The following procedure ends with a picture:

At this point, the first analysis here, it's a bit late indeed. [Email protected]~

The first time to get blog update reminders, as well as more Android dry, source code Analysis , Welcome to follow my public number, sweep the bottom QR code or long press to identify two-dimensional code, you can pay attention to.

If you feel good, easy to praise, but also to the author's affirmation, can also share this public number to you more people, original not easy

Android View Framework Summary (vii) View event distribution mechanism

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.