Android left and right swipe event monitoring

Source: Internet
Author: User

import android.os.bundle;  import android.app.activity;  import  android.content.context;  import android.util.log;  import  android.widget.relativelayout;    public class mainactivity extends  activity {       @Override       protected  void oncreate (bundle savedinstancestate)  {           super.oncreate (savedinstancestate);           setcontentview (R.layout.activity_main);//The XML here is a blank layout                     //need to monitor the view   of left and right sliding events         RelativeLayout view =  (relativelayout)   This.findviewbyid (r.id.layout);                      //setlongclickable is a must.           view.setlongclickable (True);           view.setontouchlistener (New mygesturelistener (this));       }            /**       *  inherit Gesturelistener, rewrite left and right methods        */      private class MyGestureListener extends  gesturelistener {          public  Mygesturelistener (Context context)  {               super (context);          }              @Override           public boolean left ()  {              LOG.E ("Test",  "Slide Left");              return  Super.left ();          }              @Override            Public boolean right ()  {               LOG.E ("Test",  "slide Right");               return super.right ();           }       }  }  import android.content.Context;   Import android.view.gesturedetector. simpleongesturelistener;  import android.view.gesturedetector;  import  android.view.view;  import android.view.view.ontouchlistener;  import  android.view.motionevent;    /**  *  to monitor left and right sliding events, Which view is needed when the direct Setontouchlistener can be used   *  @author  linzhiquan  *  */   public class gesturelistener extends simpleongesturelistener implements  OnTouchListener {      /**  the shortest distance between left and right sliding  */       private int distance = 100;      /**  Maximum speed of left/right swipe  */      private int velocity = 200;             private GestureDetector  Gesturedetector;         &nbsP;  public gesturelistener (Context context)  {           super ();           gesturedetector  = new gesturedetector (context, this);      }         /**      *  the method that is called when the left slide, subclasses should be rewritten        *  @return       */       public boolean left ()  {           return false;      }             /**      *  to the right when you call the method, subclasses should override        *  @return       */      public  boolean right ()  {          return false;       }             @Override        public boolean onfling (motionevent e1, motionevent e2, float  velocityx,              float  VELOCITYY)  {          // todo auto-generated  method stub          // e1: a 1th Action_down  motionevent          // e2: The Last action_move  Movement speed on the motionevent          // velocityx:x axis (pixels/sec)   Movement speed on the          // velocityy:y axis (pixels/sec)              //  left           if  (E1.getx ()  - e2.getx ()  > distance                   && math.abs (Velocityx)  > velocity)  {              left ();           }          / /  Right Slide           if  (E2.getx ()  - e1.getx ()  > distance                   && math.abs (Velocityx)  > velocity)  {               right ();           }           return false;       }         @Override       public  boolean ontouch (view v, motionevent event)  {           // TODO Auto-generated method stub           gesturedetector.ontouchevent (Event);           return false;      }         public int getdistance ()  {           return distance;      }         public void setdistance (int distance)  {           this.distance = distance;      }         public int getvelocity ()  {           return velocity;      }         Public void setvelocity (int velocity)  {           this.velocity = velocity;      }         public gesturedetector getgesturedetector ()  {           return gestureDetector;      }         public void setgesturedetector (GestureDetector  Gesturedetector)  {          this.gesturedetector =  gesturedetector;      }  } 


Android left and right swipe event monitoring

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.