Android interview is required to ask about event distribution of a View, and Android view event Distribution

Source: Internet
Author: User

Android interview is required to ask about event distribution of a View, and Android view event Distribution

I really want to prepare many things for the interview. In fact, I think it is difficult for a programmer to become a programmer. It is possible to have an interviewer, I don't even know .................. however, to prove that you have learned Android, you have to answer the question. In addition to the interview, you will also be given a review basis, if you read this article, I think you 'd better organize things in your own language and pass through the brain to make your memory faster.


The interviewer asked: Could you explain how incident distribution is going?


Cainiao: I will explain it in a small example ...........

First:

1. the test priority is Ontouch> OnClick. I first register a click event (onClick) with a touch event (setOnTouchListener) and print the output result. The touch event is judged more often, for example, when the press is lifted, move events. Then I click the button to print the result. The Ontouch event is executed first, and then the click event is executed. The Ontouch event is executed twice, ACTION_DOWN, ACTION_UP, and then transmitted to OnClick.

2. Change the OnTouch return value to pass the event deadline
The Ontouch event has a returned value. I tried to change the returned value to true. I found that the Onclick event was not executed. This is because the onTouch method returns true and considers this event to be consumed by onTouch, so it will not continue to pass down

3. from the source code analysis, if I click any control, it will certainly call dispatchTouchEvent. When we click the Button, this method will be called. If the button does not happen, it will be passed to the parent class textView. If there is no textView, go to the parent class to find the View and find the method.

For the dispatchTouchEvent method in View, in this method, we first make a judgment, which contains three conditions. If all three conditions are met, true is returned, otherwise, the onTouchEvent method execution result is returned.


The first condition is a mOnTouchListener variable, which is assigned a value in the setOnTouchListener method in the View. That is to say, as long as we register the touch event for the control, mOnTouchListener must be assigned a value.

The second condition is to determine whether the currently clicked control is enable. The button is enable by default, so this condition is constant to true.

The third condition is the most critical. mOnTouchListener. onTouch (this, event) is actually the onTouch method when the callback control registers the touch event. That is to say, if we return true in the onTouch method, all three conditions will be set up, so that the entire method returns true directly. If we return false in the onTouch method, the onTouchEvent (event) method will be executed again.



4. onTouchEvent Method

If our control can be clicked, it will enter the switch judgment, and if the current event is raised, it will enter the MotionEvent. ACTION_UP case. After various judgments, the semi mclick () method is executed.

For the merge mclick () method, The onClick method is called as long as mOnClickListener is not null.

As we have just said, when we call the setOnClickListener method to register a click event for the control, it will assign a value to mOnClickListener. Then, when a control is clicked, The onClick method of the clicked control is called back in the receivmclick () method.

In this way, we can figure out the entire event distribution process of the View!



5. Three questions about touch event distribution: 1. What are the differences between onTouch and onTouchEvent?

From the source code, we can see that both methods are called in View dispatchTouchEvent, and onTouch takes precedence over onTouchEvent. If the onTouch method returns true to consume the event, the onTouchEvent will not be executed.

In addition, onTouch requires two prerequisites for execution. The value of the first mOnTouchListener cannot be blank, and the control currently clicked must be enable. Therefore, if a control is not enable, registering an onTouch event for it will never be executed. For this type of control, if we want to listen to its touch event, we must rewrite the onTouchEvent method in the control.





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.