Sliding between the left and right is the most common action of a smartphone. It is encapsulated here and can be used directly in the future.
A few simple lines are required. The class below 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 {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // The xml here is a blank layout // viewRelativeLayout view that needs to listen to the left and right sliding events = (RelativeLayout) this. findViewById (R. id. layout); // setLongClickable is a required view. setLongClickable (true); view. setOnTouchListener (new MyGestureListener (this);}/*** inherits GestureListener and overwrites the left and right methods */private class MyGestureListener extends GestureListener {public MyGestureListener (Context context Context) {super (context) ;}@ Overridepublic boolean left () {Log. e ("test", "sliding left"); return super. left () ;}@ Overridepublic boolean right () {Log. e ("test", "slide right"); 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;/*** monitors events that slide between the left and right, you can use * @ author LinZhiquan **/public class GestureListener extends SimpleOnGestureListener implements OnTouchListener {/** the shortest distance between the left and right slides */private int distance = 100; /** maximum Sliding Speed of left and right */private int velocity = 200; private GestureDetector gestureDetector; public GestureListener (Context context) {super (); gestureDetector = new GestureDetector (context, Context, this);}/*** indicates the method called when sliding to the left. The subclass should overwrite * @ return */public boolean left () {return false ;} /*** indicates the method called when sliding to the right. The subclass should override * @ return */public boolean right () {return false;} @ Overridepublic boolean onFling (MotionEvent e1, motionEvent e2, float velocityX, float velocityY) {// TODO Auto-generated method stub/e1: 1st ACTION_DOWN MotionEvent // e2: The last ACTION_MOVE MotionEvent // velocityX: moving speed on the X axis (pixel/second) // velocityY: moving speed on the Y axis (pixel/second) // sliding to the left if (e1.getX ()-e2.getX ()> distance & Math. abs (velocityX)> velocity) {left () ;}// slide right if (e2.getX ()-e1.getX ()> distance & Math. abs (velocityX)> velocity) {right ();} return false ;}@ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubgestureDetector. 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 ;}}