Android touch screen event transfer (personal practice summary), android touch

Source: Internet
Author: User

Android touch screen event transfer (personal practice summary), android touch
Android touch-screen event transfer 1: Preface due to Launcher, many interactive parts were involved, because the project has been in the past for a long time, and recently GTS and CTS tests and modifications have been made, many of them are not familiar or even forgotten. Combined with others' Article experience (I found some places are different from others, but I have been tested and verified by myself), coupled with my own tests, write down an article today to summarize it.
2. ViewGroup and its view ViewGroup are a container class. In general, the layout container inherits the word. A view can be stored in a container, or nested containers, such as Relativelayout and LinearLayout. The view I mentioned here inherits only the view class and does not inherit the ViewGroup class ). This is because the small difference in the transfer of the event that falls below is related to this. The class inherited from ViewGroup has interceptTouchEvent (), but does not inherit the ViewGroup class. This method is used to intercept touch events. 3. Event passing 1: Generally, there are three methods for event passing for ViewGroup.

  • DispatchTouchEvent (); is responsible for event distribution. If the returned value is true, it means that the event does not continue to be distributed or consumed (onTouchEvent () is executed. If onTouchEvent () returns true, it means the consumption is complete, false indicates that the consumption is not completed). Then, execute the dispatchTouchEvent () of the container class at the first level and the returned value for each level is true. If the value of dispatchTouchEvent () is false, then it is handed over to the parent class and distributed to interceptTouchEvent ();

  • InterceptTouchEvent (); intercepts events. If false is returned, the touch event is not intercepted. The touch event is handled by the view at the next level. If false is returned, the touch event is handled by onTouchEvent () at the next level;

  • OnTouchEvent (); consumes an event. If the returned value is true, the event is consumed, and the event disappears. If the returned value is false, onTouchEvent () is executed at the upper level. If the returned value is false, continue to go up. If false is returned for each level, an endless loop is entered, resulting in ANR and android 5.1 will also be in an endless loop, my tests won't result in ANR (maybe the company's mobile phone is not properly adapted). To avoid this situation, you can return true in the onTouchEvent () of the Activity;
........... extends Activity{    public boolean dispatchTouchEvent(MotionEvent ev) {         return true;    }}
2: View for containers
  • DispatchTouchEvent (); is responsible for event distribution. If the returned value is true, it means that the event does not continue to be distributed or consumed (onTouchEvent () is executed. If onTouchEvent () returns true, it means the consumption is complete, false indicates that the consumption is not completed. Note that this View is generally stored in a container, but dispatchTouchEvent () of the parent container is not executed (), instead, execute the dispatchTouchEvent () of the parent container and pass it up recursively. Why? I personally think: Because ViewGroup is not inherited (not a container class), it does not have a child View. It is the same as the dispatchTouchEvent () of the parent container during processing, therefore, although there is an event distribution function, if the returned value of dispatchTouchEvent () is false, it is handed over to the parent class for onTouchEvent () consumption. In my opinion, it is best not to forcibly return false in dispatchTouchEvent, and perform UI processing.

  • OnTouchEvent (); consumes an event. If the returned value is true, the event is consumed, and the event disappears. If the returned value is false, onTouchEvent () is executed at the upper level. If the returned value is false, continue to go up. If false is returned for each level, an endless loop is entered, resulting in ANR and android 5.1 will also be in an endless loop, my tests won't result in ANR (maybe the company's mobile phone is not properly adapted). To avoid this situation, you can return true in the onTouchEvent () of the Activity;

3: dispatchTouchEvent () and onTouchEvent () of the Activity. An event starts with the dispatchTouchEvent () method of the Activity and is passed to the view in DecorVIew (setContentView.

Waiting for code

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.