Imagine a layout with 4 buttons
_______________________________
| | |
| A | B |
|______________|________________|
| | |
| C | D |
|______________|________________|
I’d like to detect the fling gesture over the whole layout but when the fling starts over a button is no detected.
I’m using:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gesturedetector= new GestureDetector(this, this);
findViewById(R.id.touchContainer).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("","TouchEvent");
return gesturedetector.onTouchEvent(event);
}
});
}
It when there is no clickable items but fails if the fling start over a clickable item.
How can I solve that? Offering a bounty of 50 point for a complete working answer
One way I have achieved this is to override the following method:
You can override this method in your layout container (e.g. ViewGroup, or whatever you’re holding the buttons with) and continue to return
falsefrom it in order to ‘intercept’ touch events that are being consumed by childViews (i.e. your buttons). Within that overridden method you can then call your gesture detector object with theMotionEvents. This method also ‘sees’ events that target theViewGroupitself as well, which means – if I remember correctly – you would only need to call your gesture detector from within that method, and in doing so the gesture detector will ‘see’ all events, no matter whether they’er over the buttons or not. So if you drag your finger starting over a button and then ending at some point on the layout background, the gesture detector should see the entire swipe. You would not need to feed the gesture detector with the events from the layout’s ownonTouchEvent()because it’ll have already seen them.A second way:
I just looked at my project where I used this, and realised that I switched to a different way of doing it. What I actually did was I designed all of my child
Views such that the parentActivity(or the containingViewGroup) could register the same gesture detector object with all of those childViews (each of my specialViews have a method calledregisterGestureDetector()). Then, in the overridden ‘onTouchEvent()’ in my childViews, I pass theMotionEvents to the gesture detector that has been registered with thatView. In other words, the parentViewGrouplayout and all the childViews simply share the same gesture detector.I realise that this may sound like a bit of hassle and not necessary considering it could be done using
onInterceptTouchEvent(), but my application deals with some pretty complicated rules regarding how myViews need to respond to touch events and gestures, and it allowed me to apply some additional logic that I needed specific for my application. However, both of these methods I’ve used achieve the same basic objective here: to channel theMotionEvents that targetted variousViews to the same gesture detector object.