views:

119

answers:

1

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.

A: 

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.

BeRecursive
Hi Be, thanks for the quick response, I do have a couple of questions though. a) I'm not extending the viewFlipper so can I still use this? b) Will this allow me to detect a swipe gesture whilst still allowing the child views to have scroll functionality [I can currently achieve this with the activities dispatchTouchEvent]
ifuller1
The ViewFlipper implements the ViewGroup interface so no matter what the children are the ViewFlipper can use onInterceptTouchEvent. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
BeRecursive
Thanks again but your explanation it doesn't seem to qualify for my questions. Explicitly - how can I get access to the onInterceptTouchEvent without extending the viewFlipper (which I don't want to do)?
ifuller1
In short you can't. Since onInterceptTouchEvent is provided via an inheritance to ViewFlipper you have to override it
BeRecursive
As I don't want to extend the viewFlipper I've added setOnTouchListener to all the child views. So long as I return false I get the correct behaviour.
ifuller1