SoFunction
Updated on 2025-03-04

Android gestures swipe left and right

Recently, I want to implement the function of Android swiping left pop-up menu box and swiping right to disappear menu. I learned about the sliding event of Android. It must be implemented on the view component or Activity. At the same time, the two interfaces of OnTouchListener and OnGestureListener must be implemented.

public class MyRelativeLayout extends RelativeLayout implements {
  private float mPosX, mPosY, mCurPosX, mCurPosY;
  private static final int FLING_MIN_DISTANCE = 20;// Minimum distance to move  private static final int FLING_MIN_VELOCITY = 200;// Maximum movement speed  //Build gesture detector  GestureDetector mygesture = new GestureDetector(this);
  public MyRelativeLayout(Context context){
    super(context)
  }

  public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
  }

  public MyRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
  }
    @Override
  public boolean onTouchEvent(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return (arg0);

  }

  @Override
  public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
              float distanceY) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }


  @Override
  public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

  }

   @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)
    // The coordinate displacement of the X-axis is greater than FLING_MIN_DISTANCE, and the movement speed is greater than FLING_MIN_VELOCITY pixels/second    //left    if (() - () > FLING_MIN_DISTANCE){   
//           && (velocityX) > FLING_MIN_VELOCITY) {   
      collapse();
       }  
    //up    if (() - () > FLING_MIN_DISTANCE   
           && (velocityX) > FLING_MIN_VELOCITY) {

    }   
      return false;   
  } 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.