我有一个只有一个 Activity 的 Android 应用程序。此 Activity 使用此布局:
<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">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
然后,在我的源代码中,我在构造函数中定义了两个手势检测器:
mDetector = new GestureDetectorCompat(this, new CustomGestureListener());
mScaleDetector = new ScaleGestureDetector(this, new CustomScaleGestureListener());
我用这种方式覆盖了 onTouchEvent:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() > 1) {
this.mScaleDetector.onTouchEvent(event);
} else {
this.mDetector.onTouchEvent(event);
}
return super.onTouchEvent(event);
}
我的问题是没有检测到手势(虽然我可以用滑动手势打开抽屉)。如果我将 drawerlayout 替换为线性布局,则不会出现此问题,因此原因是抽屉导航。我做错了什么?
最佳答案
您必须为抽屉布局设置 TouchListener。 例如/
gestureDetector = new GestureDetector(this, new CustomGestureListener());
mDrawerLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
关于java - 在 Android 应用程序中使用 DrawerLayout 时手势不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17882255/