通常按下 的内容区域时DrawerLayout
,抽屉将关闭并消耗触摸。有没有办法防止这种情况并将触摸事件传递到内容区域?
谢谢!
通常按下 的内容区域时DrawerLayout
,抽屉将关闭并消耗触摸。有没有办法防止这种情况并将触摸事件传递到内容区域?
谢谢!
我最终修改了 DrawerLayout。
在该方法onInterceptTouchEvent(MotionEvent ev)
中,您必须防止interceptForTap
被设置为 true。一种方法是删除以下条件。
if (mScrimOpacity > 0 &&
isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) {
interceptForTap = true;
}
这将允许触摸“失败”。
要让抽屉不关闭,您可以将抽屉锁定模式设置为LOCK_MODE_LOCKED_OPEN
。
受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 );
}
...
}