问题
I have an app that overrides the onTouchEvent(MotionEvent ev)
of the MainActivity
to determine Two-Finger-Swipe
and Pich-Open
/Pinch-Close
.
Everything works fine until I add the DrawerLayout
to the app (like it's described in Creating a Navigation Drawer). Problem: the DrawerLayout
prevents the call of onTouchEvent()
in the MainActivity.
I started to write a CustomDrawerLayout
, and try to override the DrawerLayout
methods onInterceptTouch()
and onTouchEvent()
.
The only way (I found) to transmit the TouchEvent
to the MainActivity:
// onTouchEvent of CustomDrawerLayout
@Override
public boolean onTouchEvent(MotionEvent ev){
// super.onTouchEvent(ev); // prevent transmission of TouchEvent
return false;
}
The problem here is that the Drawer doesn't open correctly. The Drawer stucks like described in this post: DrawerLayout getting stuck on swipe.
Is it possible to transmit the TouchEvent
to MainActivity to handle the MultiTouchDetection? Or do I have to handle this in CustomDrawerLayout
?
UPDATE 1
First I have to say that the Drawer only stucks, if I swipe from the left edge. By clicking on the DrawerIcon in the ActionBar
the Drawer works fine.
Transmission of TouchEvent
works with following code. But only if the Drawer is opend! Otherwise Activity.onTouchEvent
isn't called!
// onTouchEvent of CustomDrawerLayout
@Override
public boolean onTouchEvent(MotionEvent ev){
// super.onTouchEvent(ev); // still prevents transmission of TouchEvent
activity.onTouchEvent(ev);
return true;
}
By opening the Drawer with a swipe from the edge (-> Drawer stucks) i get a really strange behaviour :
- just DrawerIcon can close "stucked Drawer" (-> I wouldn't expect something different because I override the
CustomDrawerLayout.onTouchEvent
) - if I close the stucked Drawer by DrawersIcon the
CustomDrawerLayout.onTouchEvent
is still called
Thats strange! Why Activity.onTouchEvent()
isn't called? And how can I prevent the stucked Drawer?
UPDATE 2
Now i override the CustomDrawerLayout.onInterceptTouch()
:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
return true;
}
This has the effect that the Drawer can't be opened by a swipe from the edge -> only the DrawersIcon can open and close the Drawer. But now the TouchEvent
is always transmitted to the Activity (-> that works like expected).
But what I really want is to have the possibilty to open the Drawer by a swipe from the edge AND to have my MultiGestureDetector. Is this possible?
回答1:
A bit of a late update, but after having this issue for a couple of days, the solution that worked best for me was creating a CustomDrawerLayout. Then, casting the Context from the constructor as an Activity, called the Activity onTouchEvent from onInterceptTouchEvent.
@Override public boolean onInterceptTouchEvent( MotionEvent ev )
{
getActivity().onTouchEvent( ev );
return super.onInterceptTouchEvent( ev );
}
I find the code to be a lame hack... but works for me. Good luck!
回答2:
I was having some issues recognizing gestures for items within the navigation drawer itself and came across requestDisallowInterceptTouchEvent. What I found was that I was getting the MotionEvent.ACTION_DOWN, but nothing after that because the NavigationDrawer was intercepting the touches. The key would be to call requestDisallowInterceptTouchEvent(true) on the down event in your touch handler in the activity so that you can handle the touch without it being intercepted.
This presentation was also extremely useful when trying to figure out the touch system in Android.
回答3:
In case you or anyone else still need it, here is my implementation.
Works perfectly with the exception of the "edge" not being taken from android code but as a constant (which I set).
Please view this link:
Android Navigation Drawer Doesn't Pass onTouchEvent to Activity
来源:https://stackoverflow.com/questions/21040150/drawerlayout-prevents-call-of-mainactivity-ontouchevent