Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - detect gesture on different views

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!

like image 522
Anthea Avatar asked Jun 23 '26 03:06

Anthea


1 Answers

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

like image 57
ACM64 Avatar answered Jun 24 '26 16:06

ACM64