The right and left sliding is the most commonly used mobile phone, this simple encapsulation a bit, later directly to use on it.
Just a few lines is all it takes, and the following class is encapsulated.
package com.example.test;
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.oncre
Ate (savedinstancestate); Setcontentview (R.layout.activity_main);//The XML here is a blank layout//view Relativelayout view of the left and right sliding events (relativelayout
) This.findviewbyid (r.id.layout);
Setlongclickable is a necessary view.setlongclickable (true);
View.setontouchlistener (This) (new Mygesturelistener); /** * Inherits Gesturelistener, overrides left and right methods * * Private class Mygesturelistener extends Gesturelistener {public mygest
Urelistener (context) {super (context);
@Override public Boolean Left () {LOG.E ("test");
return Super.left ();
@Override public Boolean Right () {LOG.E ("test", "slide to starboard");
return Super.right (); }
}
}
Package com.example.test;
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 the right and left sliding events, which view needs 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;
/** the maximum speed of sliding left and right/private int velocity = 200;
Private Gesturedetector Gesturedetector;
Public Gesturelistener {super ();
Gesturedetector = new Gesturedetector (context, this);
/** * The method to invoke when sliding to the left, subclasses should override * @return * */public Boolean Left () {return false;
/** * The method called when sliding to the right, the subclass 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:1th action_down motionevent//E2: Move speed on last Action_move//motionevent axis (Pixel/sec)//velocityx:x movement speed on the axis
Degrees (Pixels/sec)//left slip if (E1.getx ()-e2.getx () > Distance && math.abs (Velocityx) > Velocity) () ();
//Rightwards 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.on
TouchEvent (event);
return false;
public int getdistance () {return distance;
The 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;
}
}