3
votes

I want to listen to touch events for a viewFlipper. I've been able to listen to touch events in my activity and then modify the viewFlipper but these events are fired wherever the user is within the activity and I need to capture touch events specifically on the viewFlipper. I have tried adding setOnTouchListener but it is not called. I'm assuming the viewFlippers children (webviews) are 'consuming' the touch events.

One solution would be to setOnTouchListener's to each of the webviews but this feels like a hack. Does anyone know another way?

Thanks,

Ian

Sorry if this is a double post - but my previous post seems to have vanished.

3
What about ViewGroup.onInterceptTouchEvent(MotionEvent)?BeRecursive
@user440556: "I'm assuming the viewFlippers children (webviews) are 'consuming' the touch events." -- correct.CommonsWare
So intercept the touch event using the above callBeRecursive

3 Answers

4
votes

Use ViewGroup.onInterceptTouchEvent(MotionEvent)

You should Reference the Android Documentation as it's usage is quite complicated.

Basic Summary of use:

You receive the touch event here. If you want to consume it, return true and control will be passed to the ViewFlipper's onTouchEvent(). Return false and it will continue to be passed to the child. onTouchEvent() should also return true to ensure all further events are returned to the ViewFlipper's method. The child will also receive the original event with the action ACTION_CANCEL.

4
votes

Finally It worked for me. Return true by default to get multiple calls on this listener.

viewFlipper.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (this.gestureDetector.onTouchEvent(event)) {
                return false;
            }
            return true;
        }
  });
1
votes

I was having the same problem and found your page trying to google for an answer. After a few very frustrating attempts I ended up finding a quite easy solution, I'm still listening to the touch on the whole activity just like you did, but on the OnTouchEvent I filter if the ViewFlippers is touched or not:

@Override
public boolean onTouchEvent(MotionEvent event) {
        if(mFlip.isInTouchMode()){
            return gestureDetector.onTouchEvent(event);
        } else{
            return super.onTouchEvent(event);
        }   
}

hope it helps!