Android custom component series [6]-advanced practices (3)

Source: Internet
Author: User

In the previous article "Android custom components series [5] -- advanced practice (2)", I continued to analyze the instructor's "Implementation of the drop-down PinnedHeaderExpandableListView, this plan inserts a "knowledge point" to parse the event distribution mechanism in Android. Careful friends may find that many components in the Android project written by Daniel are customized (this is why the interface and experience attract you so much ), however, to flexibly customize components, you must be familiar with gestures (listener) and be able to handle the relationship between events.

First look at a piece of code:

 

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button = (Button) findViewById(R.id.button);                 button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Log.i(TAG, "onClick");}});                button.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:Log.i(TAG, "onTouch  down");break;case MotionEvent.ACTION_MOVE:Log.i(TAG, "onTouch move");break;case MotionEvent.ACTION_UP:Log.i(TAG, "onTouch up");break;default:break;}return false;}});    }

 

We can see that the onTouch method will be called first before calling the onClick method. If we change the returned value of the onTouch method to true, The onClick method will not be called and the event will be consumed by the onTouch method.

Do you still remember to use the dispatchTouchEvent method in the previous articles.

 

public boolean dispatchTouchEvent(MotionEvent event) {    if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&            mOnTouchListener.onTouch(this, event)) {        return true;    }    return onTouchEvent(event);}
We can see that the onTouch method is called in dispatchTouchEvent, so it will be called before the onClick method. If true is returned by onTouch, dispatcheTouchEvent returns true without executing other methods.

 

 

In Android, each ViewGroup subclass has the following three methods:

Public boolean dispatchTouchEvent (MotionEvent event): used to distribute TouchEvent

Public boolean onInterceptTouchEvent (MotionEvent event): Used to intercept TouchEvent

Public boolean onTouchEvent (MotionEvent event): Process TouchEvent

 

 
     
              
               
                
                
       
  
 

 


After an ACTION_DOWN event occurs, the system calls the dispatchTouchEvent method of the Activity to distribute the event. Based on the coordinates of the touch event, the event is passed to the dispatchTouchEvent processing of the out (outermost layer. The onInterceptTouchEvent method is called to determine whether the event is handled by itself, or to pass it down to the subview. If the event is not handled, the subview is directly distributed to the subview Based on the coordinates generated by the event.

In the figure, the center component is a clickable component, indicating that the Touch event can be processed. Therefore, the onInterceptTouchEvent method in the center transmits the event to the center.

In TouchEvent, if the returned value is true, it indicates that the event is consumed and will not be passed down. If the returned value is false, the event is not consumed and will be passed on. If the center does not process events (android: clickable = "false") and the events are not consumed by the onTouchEvent of the center, the events are reversely returned to the activity layer by layer.

 

Let's take a look at the event distribution mechanism. Next, we will analyze it ......

 

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.