因此,我希望能够使用GestureOverlayView和OnGestureListener识别高级手势,并能够使用GestureDetector检测两次敲击和长按。我的两个功能在两个不同的项目中分别可以正常工作。但是,我试图将它们结合起来并遇到一些问题。看来,当我尝试双击/长按时,由于GestureOverlayView会覆盖整个屏幕,并且只会识别在Gesture Builder中定义的高级手势,因此无法识别它。有谁知道如何设置GestureOverlayView,以便允许GestureDetector完成任务?我的代码:
public class HelloAndroid extends Activity implements OnGesturePerformedListener, OnDoubleTapListener, OnGestureListener {
/** Called when the activity is first created. */
private GestureLibrary mLibrary;
private GestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
detector = new GestureDetector(this, this);\
还有xml ...
<?xml version="1.0" encoding="utf-8"?>
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:visibility="invisible"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
</android.gesture.GestureOverlayView>
提前致谢!
最佳答案
我做到了,但是不确定我的解决方案是否对您有用。
您是否需要在整个 Activity 中或仅在列表中检测双击并长按?
在我的项目中,我所拥有的当然是包装所有内容的GestureOverlayView,但是我的GestureDetector仅分配在我要处理这些事件的容器上,特别是在带有缩略图的Grid上。
实际上,我的手势检测器位于View.OnTouchListener中
public class TouchListener implements View.OnTouchListener {
Rect rect = new Rect();
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
// We do not use the return value of
// mGestureDetector.onTouchEvent because we will not receive
// the "up" event if we return false for the "down" event.
// false is returned so children views can receive touch event too.
return false;
}
private View getViewAt(int x, int y) {
// I need to know which thumbnail was selected
}
GestureDetector gestureDetector = new GestureDetector(new SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e) {…}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {…}
@Override
public boolean onDoubleTap(MotionEvent e) {...}
});
}
然后,我只需要将此触摸监听器分配到我的网格即可。就您而言,它可能会在您的列表中使用。
mGrid.setOnTouchListener(new TouchListener());
发生的情况是GesturOverlayView将在未检测到手势的情况下将触摸事件传播到内部 View 。解决方案是在需要使用监听器onTouch方法上的GestureDetector检测到简单手势的 View 上创建一个触摸监听器。希望对您有所帮助。
关于android - GestureOverlayView和双击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4930423/