开发者

setOnTouchListener not called on ViewFlipper

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 t开发者_开发问答ried 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.


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.


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;
        }
  });


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜