Android self-defined component family "6"-Advanced Practice (3)

Source: Internet
Author: User

The previous article "Android own definition Component series" 5 "-Advanced Practice (2)" continue to the teacher's "can pull down the realization of the Pinnedheaderexpandablelistview" analysis, the plan in the middle of a "knowledge point", Resolves the event distribution mechanism in Android. Attentive friends may find that opening an Android project written by Daniel, a lot of components are defined by themselves (that's why the interface and experience are so appealing to you), but to be flexible in defining components, you must be familiar with gestures (that is, all kinds of listening) and be able to handle the relationship between events.

Look at the code first:

@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"  ); 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;}});    

You can see that the Ontouch method is called first. The OnClick method is then called, assuming that we change the return value of the Ontouch method above to True, the OnClick method will not be called and the event will be consumed by the Ontouch method.

Remember that a dispatchtouchevent method was used in the previous articles.

public boolean dispatchtouchevent (Motionevent event) {    if (montouchlistener! = null && (Mviewflags & Enab Led_mask) = = ENABLED &&            montouchlistener.ontouch (this, event)) {        return true;    }    Return Ontouchevent (event);}
You can see that the Ontouch method is called in dispatchtouchevent, so it is called before the OnClick method. Assuming Ontouch returns True, Dispatchetouchevent will return true immediately and no other methods will be run.

Every ViewGroup subclass in the Android system has three methods, such as the following:

public boolean dispatchtouchevent (Motionevent event): Used to distribute TouchEvent

public boolean onintercepttouchevent (Motionevent event): Used to intercept touchevent

public boolean ontouchevent (Motionevent event): Handling TouchEvent

<?xml version= "1.0" encoding= "Utf-8"?

><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent "android:layout_height=" fill_parent "android:orientation=" vertical "> <com.kris.touch.widget.touchvie W android:id= "@+id/view_out" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android:background= "#fff" android:gravity= "center" > <com.kris.touch.widget.touchview Android:id= "@+id/view_mid" android:layout_width= "300px" android:layout_height= "400px "Android:background=" #f00 "android:gravity=" center "> <com.kris.touch.widge T.touchview android:id= "@+id/view_center" android:layout_width= "150px" Androi d:layout_height= "150px" android:background= "#000" android:gravity= "center" an Droid:clickable= "true" > </com.kris.touch.widget.TouchView> </com.kris.touch.widget.TouchView> &LT;/COM.KRIS.TOUCH.WIDG Et. Touchview></linearlayout>


The system calls the activity's Dispatchtouchevent method after the first touch event (Action_down) occurs. Distribute the event.

This event is passed to the out (outermost) Dispatchtouchevent processing based on the coordinates of the touch event. Out calls the Onintercepttouchevent method to infer whether the event is handled by itself. or pass it down to the child view. Assuming out does not handle the event is distributed to its immediate child view based on the event generation coordinates.

The center component in the diagram is a clickable (clickable) component that represents the ability to handle touch events, so the Onintercepttouchevent method in center passes events to the center


TouchEvent, assuming that the return value is true, it means that the event is consumed (consumed) and is no longer passed down. Assuming that the return value is false, there is no consumption event and will continue to pass. Assume that the event is not handled in center (android:clickable= "false"). Events are not consumed by the ontouchevent of the center, and events are reversed back to the activity layer.


The event distribution mechanism is understood here first. The next article goes on to analyze ...


Android self-defined component family "6"-Advanced Practice (3)

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.