3 Methods for Android event delivery and handling of viewpager nesting issues

Source: Internet
Author: User
Tags gety

The Dispatchtouchevent method is used for the distribution of events, and all events in Android must be distributed through this method, and then decide whether to consume the current event itself or continue to distribute it to child control processing. Returns true to indicate that the distribution is not continued and the event is not consumed. Returns false to continue the distribution, if ViewGroup is distributed to onintercepttouchevent to determine whether to intercept the event.

The Ontouchevent method is used to handle the event, which returns true to indicate that the consumer handles the current event, returns false and does not process, and gives the child control to continue distribution.

Onintercepttouchevent is a viewgroup in the method, the view is not, it is responsible for the interception of events, return true to intercept the current event, do not continue to distribute, to their own ontouchevent to handle. return false to Not intercept, continue to pass. This is a unique method for ViewGroup, because there may be sub-view in ViewGroup, and in Android view it is no longer possible to include child view (iOS can).

---------------------------------the processing of viewpager nesting problems---------------------------------------

package com.lwj.app.customview;import android.content.context;import android.graphics.pointf; import android.support.v4.view.viewpager;import android.util.attributeset;import  android.view.motionevent;public class childviewpager extends viewpager{     /**  touch-pressed point  **/    pointf downp = new pointf ();     /**  Touch the current point  **/    PointF curP = new  PointF ();    onsingletouchlistener onsingletouchlistener;      public childviewpager (context context, attributeset attrs)  {         super (context, attrs);         //  TODO Auto-generated constructor stub    }      public childviewpager (context  context)  {        super (context);         // TODO Auto-generated constructor stub    }       @Override     public boolean onintercepttouchevent ( MOTIONEVENT ARG0)  {        // todo auto-generated  method stub        //when the Intercept touch event arrives at this position, return to true,   The       //description intercepts Ontouch in this control, which in turn executes the ontouchevent      of this control    return true;    }      @Override      public boolean ontouchevent (motionevent arg0)  {         // TODO Auto-generated method stub         //every time the Ontouch event is recordedCurrent pressed coordinates         curp.x = arg0.getx ();         curp.y = arg0.gety ();          if (Arg0.getaction ()  == motionevent.action_down) {             //record the coordinates at the time of the press              //remember not to use  downp = curp&nbsp, so when changing curp, DOWNP will also change              downp.x = arg0.getx ();             downp.y = arg0.gety ();             //This code is to inform his father Viewpager is now doing the operation of this control, do not interfere with my operation              getparent (). Requestdisallowintercepttouchevent (True);         }&nBsp;        if (Arg0.getaction ()  == motionevent.action_move) {             //This code is to inform his parent that Viewpager is now doing the operation of this control, Do not interfere with my operation             getparent (). Requestdisallowintercepttouchevent (True);        }          if (Arg0.getaction ()  == motionevent.action_up) {             //at up to determine whether to press and drop the coordinates for a point              //if it is a point, the Click event will be executed, which is the Click event I wrote myself, not onclick             if (DOWNP.X==CURP.X && DOWNP.Y==CURP.Y) {                 onsingletouch ( );                 return true;             }        }          return super.ontouchevent (arg0);    }          /**     *  Click      */     public void onsingletouch ()  {         if  (onsingletouchlistener!= null)  {              onsingletouchlistener.onsingletouch ();         }    }     /**     *  creating a Click event interface       *  @author  wanpg     *      */    public interface onsingletouchlistener {         public void onsingletouch ();    }      Public void setonsingletouchlistener (Onsingletouchlistener onsingletouchlistener)  {         this.onSingleTouchListener = onSingleTouchListener;     } }


3 Methods for Android event delivery and handling of viewpager nesting issues

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.