0

// On swipe down -> RIGHT and then DOWN
// On swipe right -> RIGHT and then DOWN
// On swipe left -> LEFT
// On swipe up -> LEFT and then UP
public class DragGestureDetector {
    public static String DEBUG_TAG = "DragGestureDetector";
    private GestureDetectorCompat mGestureDetector;
    private DragListener mListener;
    private boolean mStarted = false;
    private MotionEvent mOriginalEvent;

    float x1,x2;
    float y1, y2;

    public interface DragListener {
        boolean onDragStart(MotionEvent e1, MotionEvent e2, float distanceX,
                                float distanceY);
        boolean onDragContinue(MotionEvent e1, MotionEvent e2, float distanceX,
                                   float distanceY);
        boolean onDragEnd(MotionEvent e1, MotionEvent e2);

        boolean onTapUp();
    }

    public DragGestureDetector(Context context, DragListener myDragListener){
        mGestureDetector = new GestureDetectorCompat(context,new MyGestureListener());
        mListener = myDragListener;
    }

    public void onTouchEvent(MotionEvent event){
        mGestureDetector.onTouchEvent(event);
        int action = MotionEventCompat.getActionMasked(event);
        switch(action) {
            case MotionEvent.ACTION_UP:
                if(mStarted) {
                    mListener.onDragEnd(mOriginalEvent, event);
                }
                mStarted = false;
                x2 = event.getX();
                y2 = event.getY();

                if (x1 < x2)
                {
                    System.out.println("sammy_RIGHT");
                }

                if (x1 > x2)
                {
                    System.out.println("sammy_LEFT");
                }


                if (y1 < y2)
                {
                    System.out.println("sammy_DOWN");
                }

                if (y1 > y2)
                {
                    System.out.println("sammy_UP");
                }
                break;
            case MotionEvent.ACTION_DOWN:
                //need to set this, quick tap will not generate drap event, so the
                //originalEvent may be null for case action_up
                //which lead to null pointer
                mOriginalEvent = event;
                x1 = event.getX();
                y1 = event.getY();
                break;
        }
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                                float distanceY) {
            if(mListener == null) return true;
            if(!mStarted){
                mListener.onDragStart(e1,e2,distanceX,distanceY);
                mStarted = true;
            }
            else{
                mListener.onDragContinue(e1,e2,distanceX,distanceY);
            }
            mOriginalEvent = e1;
            return true;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {

            return mListener.onTapUp();
        }
    }


}

I'm implementing DragGestureDetector from this library. I want to detect swipe action of UP, LEFT, RIGHT and DOWN properly. My current code detect only left swipe correctly. I'm showing what LOG prints in LOGCAT on swiping.

Monojit
  • 3
  • 6

1 Answers1

0

please refer below link for the answer to the your question.

Android: How to handle right to left swipe gestures

Community
  • 1
  • 1
Kishan Donga
  • 2,851
  • 2
  • 23
  • 35