通常,当按下DrawerLayout
的内容区域时,抽屉将关闭并消耗触摸。有没有办法防止这种情况并将触摸事件传递到内容区域?
谢谢!
答案 0 :(得分:5)
我最终修改了DrawerLayout。
在方法onInterceptTouchEvent(MotionEvent ev)
中,您必须阻止interceptForTap
设置为true。一种方法是删除以下条件。
if (mScrimOpacity > 0 &&
isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) {
interceptForTap = true;
}
这将使触碰“堕落”。
要使抽屉不关闭,您可以将抽屉锁定模式设置为LOCK_MODE_LOCKED_OPEN
。
答案 1 :(得分:1)
受到guy_m给出的答案的启发,我调整了他的提议并建议了以下DrawerLayout的扩展。同样,这个解决方案是重写onInterceptTouchEvent()。
重写方法的逻辑非常简单:
以下重写的onInterceptTouchEvent()方法示例适用于DrawerLayout,其抽屉视图位于屏幕右侧(android:gravity =“right”)。但是,如何使代码适用于标准的左侧抽屉视图应该是显而易见的。
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 );
}
...
}