[英]Android: How do I keep DrawerLayout from passing touch events to the underlying view
I am using the Support library's DrawerLayout
in my app. 我在我的应用程序中使用支持库的DrawerLayout
。 I noticed that, when I click on an empty area in my Drawer view, the underlying View (containing a ListView
) receives the Touch event and reacts to it. 我注意到,当我在Drawer视图中单击一个空白区域时,底层View(包含ListView
)会收到Touch事件并对其作出反应。
The onInterceptTouchEvent
method of the DrawerLayout
looks like this: 该onInterceptTouchEvent
的方法DrawerLayout
看起来是这样的:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
// "|" used deliberately here; both methods should be invoked.
final boolean interceptForDrag = mLeftDragger.shouldInterceptTouchEvent(ev) |
mRightDragger.shouldInterceptTouchEvent(ev);
boolean interceptForTap = false;
switch (action) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mInitialMotionX = x;
mInitialMotionY = y;
if (mScrimOpacity > 0 &&
isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) {
interceptForTap = true;
}
mDisallowInterceptRequested = false;
mChildrenCanceledTouch = false;
break;
}
case MotionEvent.ACTION_MOVE: {
// If we cross the touch slop, don't perform the delayed peek for an edge touch.
if (mLeftDragger.checkTouchSlop(ViewDragHelper.DIRECTION_ALL)) {
mLeftCallback.removeCallbacks();
mRightCallback.removeCallbacks();
}
break;
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
closeDrawers(true);
mDisallowInterceptRequested = false;
mChildrenCanceledTouch = false;
}
}
return interceptForDrag || interceptForTap || hasPeekingDrawer() || mChildrenCanceledTouch;
}
My view with the DrawerLayout
: 我对DrawerLayout
:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/sidebar_container"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left"/>
</android.support.v4.widget.DrawerLayout>
What can I do, (if possible without extending the DrawerLayout
class) to prevent this behaviour? 我该怎么办(如果可能的话,不延长DrawerLayout
类)来防止这种行为? As long as the drawer is open, I want no click events to reach the background view. 只要抽屉打开,我就不希望任何点击事件到达背景视图。
Set clickable to true on the drawer - it'll consume the touch. 在抽屉上设置可点击为真 - 它将消耗触摸。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/drawer_view"
android:layout_width="300dp"
android:clickable="true"
android:importantForAccessibility="no"
android:layout_height="match_parent"
android:layout_gravity="left"/>
</android.support.v4.widget.DrawerLayout>
I added android:importantForAccessibility="no"
because marking the drawer as interactive (clickable or focusable) will make the entire drawer visible to accessibility services like TalkBack. 我添加了android:importantForAccessibility="no"
因为将抽屉标记为交互式(可点击或可聚焦)将使整个抽屉对TalkBack等可访问性服务可见。
This is not what you want (usually) - more often, items inside the drawer should be accessible to the services. 这不是您想要的(通常) - 更常见的是,抽屉内的物品应该可供服务使用。
This attribute is only available on API 16+. 此属性仅适用于API 16+。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.