SoFunction
Updated on 2025-03-02

The event class sharing of the encapsulated Android monitor's fingers sliding the screen left and right

Sliding left and right is the most commonly used action for smartphones. It is simply encapsulated here and then used directly in the future.

It only takes a few lines, and the following class is encapsulated.

package ;

import ;
import ;
import ;
import ;
import ;

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);//The xml here is a blank layout 
 //The view of the sliding event on the left and right needs to be monitored RelativeLayout view = (RelativeLayout) ();
 
 //setLongClickable is a must (true);
 (new MyGestureListener(this));
 }
 
 /**
  * Inherit GestureListener, override left and right methods
  */
 private class MyGestureListener extends GestureListener {
 public MyGestureListener(Context context) {
  super(context);
 }

 @Override
 public boolean left() {
  ("test", "Swipe left");
  return ();
 }

 @Override
 public boolean right() {
  ("test", "Swipe right");
  return ();
 }
 }
}


package ;

import ;
import ;
import ;
import ;
import ;
import ;

/**
  * Implement to monitor events that slide left and right. Whenever view needs it, you can use it directly by setting OnTouchListener.
  * @author LinZhiquan
  *
  */
public class GestureListener extends SimpleOnGestureListener implements OnTouchListener {
 /** The shortest distance to slide left and right */
 private int distance = 100;
 /** Maximum speed of sliding left and right */
 private int velocity = 200;
 
 private GestureDetector gestureDetector;
 
 public GestureListener(Context context) {
 super();
 gestureDetector = new GestureDetector(context, this);
 }

 /**
  * The method called when swiping left, the subclass should be rewritten
  * @return
  */
 public boolean left() {
 return false;
 }
 
 /**
  * The method called when swiping to the right, the subclass should be rewritten
  * @return
  */
 public boolean right() {
 return false;
 }
 
 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  float velocityY) {
 // TODO Auto-generated method stub
 // e1: The first ACTION_DOWN MotionEvent // e2: The last ACTION_MOVE MotionEvent // velocityX: Movement speed on the X-axis (pixels/second) // velocityY: The movement speed on the Y axis (pixels/second)
 // Swipe left if (() - () > distance
  && (velocityX) > velocity) {
  left();
 }
 // Swipe right if (() - () > distance
  && (velocityX) > velocity) {
  right();
 }
 return false;
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
 // TODO Auto-generated method stub
 (event);
 return false;
 }

 public int getDistance() {
 return distance;
 }

 public void setDistance(int distance) {
  = distance;
 }

 public int getVelocity() {
 return velocity;
 }

 public void setVelocity(int velocity) {
  = velocity;
 }

 public GestureDetector getGestureDetector() {
 return gestureDetector;
 }

 public void setGestureDetector(GestureDetector gestureDetector) {
  = gestureDetector;
 }
}