在Android应用程序使用DrawerLayout手势时不工作 [英] Gestures not working when using DrawerLayout in Android app
问题描述
我有一个单一的活动Android应用程序。本次活动采用这种布局:
I have an Android app with a single Activity. This activity uses this layout:
<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>
然后,在我的源代码我在构造函数中定义了两个手势探测器:
Then, in my source I have two gesture detectors defined in the constructor:
mDetector = new GestureDetectorCompat(this, new CustomGestureListener());
mScaleDetector = new ScaleGestureDetector(this, new CustomScaleGestureListener());
和我重写的onTouchEvent是这样的:
And I'm overriding onTouchEvent this way:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() > 1) {
this.mScaleDetector.onTouchEvent(event);
} else {
this.mDetector.onTouchEvent(event);
}
return super.onTouchEvent(event);
}
我的问题是,手势检测不到(虽然我可以用滑动手势打开抽屉)。如果余替换为drawerlayout,例如,一个线性布局不会发生这种问题,所以起因是导航抽屉。我究竟做错了什么?
My problem is that gestures are not detected (although I can open the drawer with a swipe gesture). This problem doesn't occur if I replace the drawerlayout with, for example, a linear layout, so the cause is the navigation drawer. What am I doing wrong?
推荐答案
您必须设置一个TouchListener为你的抽屉布局。 例如:/
you have to set a TouchListener for your drawer layout. eg/
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;
}
});
这篇关于在Android应用程序使用DrawerLayout手势时不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!