3

I need to detect different gestures on more then one views. My views need to be able to receive Tap, Double Tap and Drag Events. I tried the Gesture Detector but my implementation shows me only global gesture events and I can't connect these events to a specific view.

in my activity.onCreate:

    dthandler = new DoubleTapHandler();
    mDetector = new GestureDetector(this,dthandler);
    gestureListener = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("myLog","touch");
        mDetector.onTouchEvent(event);
        return false;
    }
};

in my activity I override the dispatchTouch function:

@Override 
      public boolean dispatchTouchEvent(MotionEvent me){ 
        this.mDetector.onTouchEvent(me);
       return super.dispatchTouchEvent(me); 
      }

this is how I try to connect the touchevent with my views:

prod.setOnTouchListener(this.gestureListener);

my DoubleTapHandler:

public class DoubleTapHandler implements OnDoubleTapListener, OnGestureListener {
        private ProductView relatedView;

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {   
            Log.d("myLog", "onDoubleTapEvent");
            Log.d("myLog",""+e.getSource());
            return false;                      
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.d("myLog", "onDoubleTap"+relatedView);
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.d("myLog", "singletap");
            return false;
        }
}

Anyone has an advice? Thanks!

Anthea
  • 3,741
  • 5
  • 40
  • 64

2 Answers2

2

Crazy thought. Create a global gesture listener on some view that wraps them all. And manually call to dispatchTouchEvent on all of your views. It's tricky but it can work.

Then add onTouchEventListenrer on your views.

And if this not working, than the proper way would be: Implementing gesture listeners by your self(taps and drags shouldn't be that hard) and working with intercept touch events.

galex
  • 3,279
  • 2
  • 34
  • 46
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
2

To make it working, attach gestures directly to each View, and you can have different implementations then.

acm0x
  • 775
  • 6
  • 14
  • you did prod.setOnTouchListener(), this you can do also for particular views in order to make them react on your gestures. – acm0x May 27 '11 at 10:12
  • right i did it (prod is one of my view). but i don't receive a gesture on this view. I just receive OnTouch events. – Anthea May 27 '11 at 10:46
  • try to replace dispatchTouchEvent() with onTouchEvent(), also take a look to http://stackoverflow.com/questions/937313/android-basic-gesture-detection – acm0x May 27 '11 at 11:04
  • Would you mind adding some code to show how to do it? – Suragch Jan 15 '15 at 12:02
  • Actually, this question in 3 years old and most of the things are already reafactored and optimised, but anyway, in the original question code it is sufficient not to override dispatchTouchEvent, but to override onTouchEvent and it should work as a charm. – acm0x Jan 16 '15 at 13:17