Normally when pressing on the content area of the DrawerLayout
, the drawer will close and the touch is consumed. Is there a way to prevent this and pass the touch event on to the content area?
Thanks!
Normally when pressing on the content area of the DrawerLayout
, the drawer will close and the touch is consumed. Is there a way to prevent this and pass the touch event on to the content area?
Thanks!
Inspired by the answer given by guy_m, I adapted his proposals and suggest the following extension of DrawerLayout. Again, this solution is by overriding onInterceptTouchEvent().
The logic of the overriding method is fairly simple:
The following example of an overridden onInterceptTouchEvent() method is for a DrawerLayout whose drawer view is located on the right of the screen (android:gravity="right"). However, it should be obvious how to adapt the code to work for a standard left-placed drawer view as well.
public class CustomDrawerLayout extends DrawerLayout
{
@Override
public boolean onInterceptTouchEvent( MotionEvent event )
{
final View drawerView = getChildAt( 1 );
final ViewConfiguration config = ViewConfiguration.get( getContext() );
// Calculate the area on the right border of the screen on which
// the DrawerLayout should always intercept touch events.
// In case the drawer is closed, we still want the DrawerLayout
// to respond to touch/drag gestures there and reopen the drawer!
final int rightBoundary = getWidth() - 2 * config.getScaledTouchSlop();
// If the drawer is opened and the event happened
// on its surface, or if the event happened on the
// right border of the layout, then we let DrawerLayout
// decide if it wants to intercept (and properly handle)
// the event.
// Otherwise we don't let DrawerLayout to intercept,
// letting its child views handle the event.
return ( isDrawerOpen( drawerView ) && drawerView.getLeft() <= event.getX()
|| rightBoundary <= event.getX() )
&& super.onInterceptTouchEvent( event );
}
...
}
I ended up modifing DrawerLayout.
In the method onInterceptTouchEvent(MotionEvent ev)
you'll have to prevent interceptForTap
being set to true. One way is to remove the following conditional.
if (mScrimOpacity > 0 &&
isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) {
interceptForTap = true;
}
That will allow touches to "fall through".
To have the drawer not close you can set the drawer lock mode to LOCK_MODE_LOCKED_OPEN
.
Inspired by the answer given by guy_m, I adapted his proposals and suggest the following extension of DrawerLayout. Again, this solution is by overriding onInterceptTouchEvent().
The logic of the overriding method is fairly simple:
The following example of an overridden onInterceptTouchEvent() method is for a DrawerLayout whose drawer view is located on the right of the screen (android:gravity="right"). However, it should be obvious how to adapt the code to work for a standard left-placed drawer view as well.
public class CustomDrawerLayout extends DrawerLayout
{
@Override
public boolean onInterceptTouchEvent( MotionEvent event )
{
final View drawerView = getChildAt( 1 );
final ViewConfiguration config = ViewConfiguration.get( getContext() );
// Calculate the area on the right border of the screen on which
// the DrawerLayout should always intercept touch events.
// In case the drawer is closed, we still want the DrawerLayout
// to respond to touch/drag gestures there and reopen the drawer!
final int rightBoundary = getWidth() - 2 * config.getScaledTouchSlop();
// If the drawer is opened and the event happened
// on its surface, or if the event happened on the
// right border of the layout, then we let DrawerLayout
// decide if it wants to intercept (and properly handle)
// the event.
// Otherwise we don't let DrawerLayout to intercept,
// letting its child views handle the event.
return ( isDrawerOpen( drawerView ) && drawerView.getLeft() <= event.getX()
|| rightBoundary <= event.getX() )
&& super.onInterceptTouchEvent( event );
}
...
}
© 2022 - 2024 — McMap. All rights reserved.