Discussion on Click event Handling of Parent view and child view in Android _android

Source: Internet
Author: User
Tags stub
The type of event in Android is divided into key events and screen-touching events, and touch events are the underlying events of screen-touching events, and it is necessary to have a thorough understanding of them.

One of the simplest screen touch actions triggers a series of touches events:action_down->action_move->action_move->action_move...->action_move-> Action_up

When the screen contains a viewgroup, and this viewgroup contains a child view, how does the Android system handle touch events? is viewgroup to handle touch events, or is it a child view to handle touch events? I can only say for sure to you not necessarily. Oh, why? Look at my findings below.
Will understand.

The subclasses of each view in the Android system have the following three methods that are closely related to TouchEvent processing:
1 public boolean dispatchtouchevent (motionevent ev) This method is used to distribute TouchEvent
2 public boolean onintercepttouchevent (motionevent ev) This method is used to intercept touchevent
3 public boolean ontouchevent (motionevent ev) This method is used to process touchevent
When touchevent occurs, the first activity passes touchevent to the topmost view,
TouchEvent first arrives at the top view of the Dispatchtouchevent, which is then distributed by the Dispatchtouchevent method,
If Dispatchtouchevent returns True, it is given to the view's ontouchevent processing,
If Dispatchtouchevent returns False, the Intercepttouchevent method of this view is given to determine whether or not to intercept the event.
If the intercepttouchevent returns True, that is, the intercept is dropped, the ontouchevent is processed,
If the Intercepttouchevent returns false, it is passed to the child view, and the dispatchtouchevent of the view begins the distribution of the event.
If the event is passed to the ontouchevent of a layer of the child view, this method returns false, and the event is passed from this view, and is ontouchevent to receive.
And if the ontouchevent that is passed to the top also returns false, the event "disappears" and receives no next event.
Copy Code code as follows:

Private Layoutinflater Inflater
Public View Fristview;
Public View Secondview;
Private Myviewpager Myviewpager;
Public Viewpageradapter Mviewpageradapter;
Private list<view> views;
Public Gallery MGallery;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Inflater = Getlayoutinflater ();
Fristview = inflater.inflate (r.layout.main1, NULL);
Secondview = inflater.inflate (r.layout.main2, NULL);
views = new arraylist<view> ();
Views.add (Fristview);
Views.add (Secondview);
MGallery = (Gallery) Fristview.findviewbyid (r.id.gallery);
Mgallery.setadapter (This) (new Imageadapter);
Myviewpager = (Myviewpager) Findviewbyid (R.id.pager);
Mviewpageradapter = new Viewpageradapter (views);
Myviewpager.setadapter (Mviewpageradapter);
}
Interface List
Private list<view> views;
Public Viewpageradapter (list<view> views) {
This.views = views;
}
Destroying the interface of the ARG1 location
@Override
public void Destroyitem (View arg0, int arg1, Object arg2) {
((Viewpager) arg0). Removeview (Views.get (arg1));
}
@Override
public void Finishupdate (View arg0) {
TODO auto-generated Method Stub
}
Get the current number of interfaces
@Override
public int GetCount () {
if (views!= null)
{
return Views.size ();
}
return 0;
}
Initialize the interface of the arg1 position
@Override
Public Object Instantiateitem (View arg0, int arg1) {
((Viewpager) arg0). AddView (Views.get (arg1), 0);
Return Views.get (ARG1);
}
To determine whether the object generates an interface
@Override
public boolean isviewfromobject (View arg0, Object arg1) {
return (arg0 = = arg1);
}
@Override
public void Restorestate (parcelable arg0, ClassLoader arg1) {
TODO auto-generated Method Stub
}
@Override
Public parcelable SaveState () {
TODO auto-generated Method Stub
return null;
}
@Override
public void Startupdate (View arg0) {
TODO auto-generated Method Stub
}
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.