Android Touch Event Delivery mechanism under the detailed

Source: Internet
Author: User

Respect for the original: http://blog.csdn.net/yuanzeyao/article/details/38025165

Download resources: http://download.csdn.net/detail/yuanzeyao2008/7660997


In the previous article, I focused on the transfer of touch events in the Android source code, and now I want to use a demo and an example to learn about the Touch event processing in Andorid.

In Android, the three functions closely related to touch event distribution and processing are as follows:
(1) Public boolean dispatchtouchevent (motionevent ev)
(2) Public boolean onintercepttouchevent (motionevent ev)
(3) Public boolean ontouchevent (Motionevent event)


These three methods I have analyzed their source code in the previous article: Method 1 is mainly for the touch event distribution, Method 2 is mainly for the touch event interception, Method 3 is the Touch event processing

These three methods are mainly found in Viewgroup,view,activity, as follows:


ViewGroup

View

Activity

dispatchtouchevent

with

with

Onintercepttouchevent

has

None

None

Ontouchevent

have

have

have


Let's use a demo to see how these methods are executed:
Customizing a class: Mylayoutfirst.java

public class Mylayoutfirst extends linearlayout{  private static final String TAG = "Mylayoutfirst";  Public Mylayoutfirst (context context, AttributeSet Attrs)  {    Super (context, attrs);  }    @Override Public  Boolean onintercepttouchevent (motionevent ev)  {    LOG.W ("Yzy", "mylayoutfirst-> Onintercepttouchevent-> "+myutils.getactionname (EV));    return super.onintercepttouchevent (EV);  }    @Override Public  Boolean ontouchevent (Motionevent event)  {    log.e ("Yzy", "mylayoutfirst-> Ontouchevent-> "+myutils.getactionname (event));    Return Super.ontouchevent (event);  }    @Override Public  Boolean dispatchtouchevent (motionevent ev)  {    log.i ("Yzy", "mylayoutfirst-> Dispatchtouchevent-> "+myutils.getactionname (EV));    return super.dispatchtouchevent (EV);  }  }

Custom a class; Mylayoutsecond.java

public class Mylayoutsecond extends linearlayout{  private static final String TAG = "Mylayoutsecond";  Public Mylayoutsecond (context context, AttributeSet Attrs)  {    Super (context, attrs);  }    @Override Public  Boolean ontouchevent (Motionevent event)  {    log.e ("Yzy", "mylayoutsecond-> Mylayoutsecond-> "+myutils.getactionname (event));    Return Super.ontouchevent (event);  }    @Override Public  Boolean onintercepttouchevent (motionevent ev)  {    LOG.W ("Yzy", "mylayoutsecond-> Onintercepttouchevent-> "+myutils.getactionname (EV));    return super.onintercepttouchevent (EV);  }    @Override Public  Boolean dispatchtouchevent (motionevent ev)  {    log.i ("Yzy", "mylayoutsecond-> Dispatchtouchevent-> "+myutils.getactionname (EV));    return super.dispatchtouchevent (EV);  } }

Add to Main_layout.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "   >    <com.event.demo.mylayoutfirst         android:id= "@+id/layout_first"        android:layout_width= " Match_parent "        android:layout_height=" match_parent "        android:background=" #FF0000 "        >        < Com.event.demo.MyLayoutSecond        android:id= "@+id/layout_second"        android:layout_width= "320dip        " android:layout_height= "120dip"        android:layout_gravity= "center"        android:background= "#0000FF"        >            </com.event.demo.MyLayoutSecond>    </com.event.demo.mylayoutfirst></relativelayout >

Adding Ontouchevent method in Mainactivity

public class Mainactivity extends activity{  @Override  protected void onCreate (Bundle savedinstancestate)  {    super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main);  }  @Override Public  Boolean oncreateoptionsmenu (Menu menu)  {    //Inflate the menu, this adds items to the action Bar if it is present.    Getmenuinflater (). Inflate (R.menu.main, menu);    return true;  }    @Override Public  Boolean dispatchtouchevent (motionevent ev)  {    log.i ("Yzy", "mainactivity-> Dispatchtouchevent-> "+myutils.getactionname (EV));    return super.dispatchtouchevent (EV);  }    @Override Public  Boolean ontouchevent (Motionevent event)  {    log.e ("Yzy", "mainactivity-> Ontouchevent-> "+myutils.getactionname (event));    Return Super.ontouchevent (event);}  }

Finally, a tool class is used to convert the event ID to a string.

public class myutils{  private static final String TAG = "Myutils";  public static String Getactionname (Motionevent event)  {    string name= "";    Switch (event.getaction ())    {case      motionevent.action_down:        name= "Action_down";        break;      Case Motionevent.action_move:        name= "Action_move";        break;      Case MOTIONEVENT.ACTION_UP:        name= "action_up";        break;    }    return name;}  }

Run effect



Where the blue part is Mylayoutsecond.java, the red part is Mylayoutfirst.java
Now I'll click on the blue section: Run results


As can be seen, the event is first captured by the activity, then distributed to Mylayoutfirst,mylayoutfirst first call its own onintercepttouchevent to determine whether to intercept the event, because the default return is False, So there is no interception, so the event is distributed to the Mylayoutsecond,mylayoutsecond also through the dispatchtouchevent distribution, before distributing the same check whether intercepted, by default, is not intercepted, However, since Mylayoutsecond is not a child view, all final events have their own processing, call their own Ontouchevent method, because the method by default returns False, so that this event is not consumed, continued to pass to the Mylayoutfirst, also did not consume this event, eventually passed to the mainactivity, continue to see behind the Action_move and Action_ Up does not pass in Mylayoutfirst and Mylayoutsecond, because once an event is not processed, subsequent events are not distributed. So Action_move and action_up were directly disposed of by Mainactivity.

Let's look at the second situation below:

Mainactivity

Mylayoutfirst

Mylayoutsecond

Dispatchtouchevent

Super.dispatchtouchevent

Super.dispatchtouchevent

Super.dispatchtouchevent

Onintercepttouchevent

--

True

Super.onintercepttouchevent (EV)

Ontouchevent

Super.ontouchevent

Super.ontouchevent

Super.ontouchevent

The results of the operation are as follows:


As can be seen, the event is passed to the Mylayoutfirst after not distributed to the Mylayoutsecond, directly call their own ontouchevent, because the return is false, resulting in the event is not consumed, eventually passed to the mainactivity,
And the subsequent events were not passed on to Mylayoutfirst and Mylayoutsecond, and were handled directly by Mainactivity.


The third case:

Mainactivity

Mylayoutfirst

Mylayoutsecond

Dispatchtouchevent

Super.dispatchtouchevent

Super.dispatchtouchevent

Super.dispatchtouchevent

Onintercepttouchevent

--

True

Super.onintercepttouchevent (EV)

Ontouchevent

Super.ontouchevent

True

Super.ontouchevent

Operation Result:


Unlike the case two, Mylayoutfirst's ontouchevent returns True, which means Mylayoutfirst consumes the event, so Action_down is no longer passed to mainactivity, and Action_ Move and Action_up
Were passed on to Mylayoutfirst.


The situation in the four:

Mainactivity

Mylayoutfirst

Mylayoutsecond

Dispatchtouchevent

Super.dispatchtouchevent

Super.dispatchtouchevent

Super.dispatchtouchevent

Onintercepttouchevent

--

Super.onintercepttouchevent (EV)

Super.onintercepttouchevent (EV)

Ontouchevent

Super.ontouchevent

Super.ontoucheven

True

Operation Result:


found that all the events were passed on to Mylayoutsecond and consumed.


In fact, there are many other combinations, if you are interested in trying to change the return value of each function, look at the print results, here I will not list the ...


Finally I will provide a small demo to demonstrate how to resolve the sliding conflict with the following background:
A viewpager contains two framgent, there is a fragment inside there is a horizontallistview, how to slide conflict?
I'll just post the key code.


Horizontal= (Horizontallistview) View.findviewbyid (r.id.hscroll);    Horizontal.setontouchlistener (New Ontouchlistener ()    {            @Override public      boolean OnTouch (View arg0, Motionevent event)      {        if (event.getaction () ==motionevent.action_down)        {          Parent.requestdisallowintercepttouchevent (True);        } else if (event.getaction () ==motionevent.action_up)        {          parent.requestdisallowintercepttouchevent (false);        }        return false;      }    });

Join this code to avoid sliding conflict, as for why you can refer to my previous article, "Android Touch event delivery mechanism," The two demo examples I will upload the download

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.