I have an instance where I need a GestureDetector
in my Activity
to intercept horizontal flings. This activity also happens to contain a DrawerLayout
.
The problem I'm having is that the DrawerLayout
intercepts all touch events such that I can't even requestDisallowInterceptTouchEvents
. I can't seem to ever get a touch even in the Activity to trigger that call.
I'd like the Drawer to handle swipes from the edge when closed, and from anywhere when opened, like it's supposed to. ButI need my activity to be able to intercept touch events elsewhere when the drawer is closed.
You can use a flag. There is a method onDrawerSlide on the DrawerListener, that is called when your drawers position changes, i.e. the drawer is running to open or close itself. You can set in this method a flag indicating that the drawer is running. Then, on the onFling method of the OnGestureListener just show if the drawer is running by checking the flag. If it is not running, you can do whatever you want in the onFling method, if not, dont do anything.
I hope it helps
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html#onDrawerSlide(android.view.View, float)
Not sure if you solved this, but make sure that your onTouch(View view, MotionEvent motionEvent){...} method returns true to indicate that the initial event is handled, otherwise the drawer layout is going to get the initial touch start event and gobble the next ones no matter what you pass to requestDisallowInterceptTouchEvents.
Unfortunately the BasicGestureDetect example in the SDK returns false from this method, which is fine when nothing else is around to steal those events and you're just logging them, but if you copy pasted that code into your app expecting it to work (like me), you're not going to get anything except the initial touch start.
So this section inside MainActivity from the BasicGestureDetect example:
actionFragment.getView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gd.onTouchEvent(motionEvent);
return false;
}
});
should instead be:
actionFragment.getView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gd.onTouchEvent(motionEvent);
return true;
}
});