The Android event distribution mechanism is fully parsed, taking you from the source perspective to thoroughly understand (on)

Source: Internet
Author: User

In fact, I've been preparing to write an article on the Android event distribution mechanism, starting with my first blog, where I used the knowledge of the Android event distribution in many places. Many friends have asked me various questions, such as: Ontouch and ontouchevent What is the difference, and how to use? Why is it possible for ListView to introduce a sliding menu function that ListView can't scroll? Why does a picture in a carousel use a button instead of a ImageView? Wait a minute...... I did not give a very detailed answer to these questions, because I knew that if you wanted to understand these issues thoroughly, mastering the Android event distribution mechanism was essential, and the Android event distribution mechanism was definitely not words.

After a long period of preparation, I finally decided to start writing such an article. At present, although there are a lot of articles on the Internet, but I think there is no particular article written in detail (perhaps I have not found), most of the articles just talk about theory, and then with the demo run a bit of the results. And I'm going to take you from the source point of view analysis, I believe you can more profoundly understand the Android event distribution mechanism.

Reading source is fastidious, gradual, so we also from the simple beginning, this article first take you to explore the view of the event distribution, the next chapter to explore more difficult viewgroup of the event distribution.

Let's start now! For example, you currently have a very simple project with only one activity and only one button in the activity. You may already know that if you want to register a click event for this button, you just need to call:

Button.setonclicklistener (New 

Onclicklistener () {  
    @Override public
    void OnClick (View v) {  
        log.d (" TAG "," OnClick execute ");  
    }  
);

This is done in the OnClick method, and can be executed when the button is clicked. You probably already know that if you want to add a touch event to this button, you just need to call:

Button.setontouchlistener (New 

Ontouchlistener () {  
    @Override public
    boolean Ontouch (View V, motionevent Event) {  
        log.d ("TAG", "Ontouch Execute, Action" + event.getaction ());  
        return false;  
    }  
});

The Ontouch method can do more things than onclick, such as judging the finger press, lift, move and so on. So if I have two events registered, which one will be executed first? Let's try it and we'll see. Run the Program click the button and print the results as follows:

As you can see, Ontouch is a priority for the onclick execution, and the Ontouch executes two times, one for Action_down, one for action_up (you may also have multiple action_move execution if you shake your hand). Therefore, the order of events is passed first through Ontouch and then to onclick.

A careful friend should notice that the Ontouch method has a return value, where we return false, and if we try to change the return value in the Ontouch method to true and run again, the result is as follows:

We found that the OnClick method is no longer implemented! Why is that? You can first understand that the Ontouch method returns true in the belief that the event is consumed by the Ontouch and will not continue to pass down.

If all of the above knowledge points are clear to date, your basic usage of the Android event should be mastered. But do not be satisfied with the status quo, let us from the point of view of the source code, the principle of this phenomenon is what.

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.