Question

I have a RelativeLayout I am putting a TouchListener into using GestureDetector. I have already done and can detect double tapping but how can I add a swipe event in the view also?

private void myTapEvent(){
        RlContent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });

        gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                count++;
                doTaskHere();
                return true;
            }
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }
        });
    }

After implementing a swipe event, how can I switch between a.) allowing tapping only and disabling swiping and b.) disabling tapping and allowing swiping only.

Was it helpful?

Solution

In your GestureDetector listener, add the onFling method. Additionally, to switch between them, you will want a boolean variable in your class that can be switched.

private boolean mAllowSwipe = true; // True = swipe, no double-tap; false = double-tap, no swipe

// ...

private void switchGestureMode() {
    if (mAllowSwipe)
        mAllowSwipe = false;
    else
        mAllowSwipe = true;
}

// ...

private void myTapEvent(){
    // ...

    gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (mAllowSwipe) {
                return false;
            }
            count++;
            doTaskHere();
            return true;
        }
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (!mAllowSwipe) {
                return false;
            }
            // Your logic here
        }
    });
}

// ...

There are some examples of swipe using fling here and here, and there's plenty more if you scour Google a bit.

OTHER TIPS

In order to detect swipes (they are called FLING EVENTS) you will have to implement the android.view.GestureDetector.OnGestureListener interface. One of the methods provided by this interface is onFling. This will detect the swipes (you will have to find out the exact implementation for your required swipe event).

Regarding switching between tapping and swiping, are you going to do this using some sort of a button click event? Let me modify your code assuming that you are doing it using two buttons (ignore the syntax errors). Even in the case that you don't use two buttons then you can still just modify the two boolean values below wherever you want to disable the tapping and the swiping (the swiping and tapping won't start working till you click one of the two buttons)

boolean makeSwipe = false;
boolean makeTap =false;

//onCreate method{
makeTapButton.setOnClickListener(new android.view.OnClickListener{

@Override
public onClick(View arg0){
makeSwipe = false;
makeTap = true;
}
});

makeSwipeButton.setOnClickListener(new android.view.OnClickListener{

@Override
public onClick(View arg0){
makeSwipe = true;
makeTap = false;
}
});

private void myTapEvent(){
    RlContent.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });

    gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {

            if(makeTap)
            {
            count++;
            doTaskHere();
            return true;
            }

            return false;
        }
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    });
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
            if(makeSwipe)
            {
             // Do some stuff
             return true;
            }
    return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top