正如我在标题中提到的,app 也有 aScrollView
和 a GestureDetector
。在 ScrollView 的触摸事件之外,GestureDetector 处理从左到右和从右到左的滑动操作。他们都运作良好。
现在我想在GestureLibrary
Activity 中添加一个 -I mean raw-。我查看了不同的来源并以某种方式正确添加。简而言之,布局如下所示:
<android.gesture.GestureOverlayView
android:id="@+id/gOverlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="@+id/content_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none">
</ScrollView>
<!-- Other views -->
</android.gesture.GestureOverlayView>
它按照我的意愿绘制(黄线),但它没有触发任何方法。这里我是如何实现的OnGesturePerformedListener
:
/*
* Gestures
*/
gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gLibrary.load()) { finish(); }
GestureOverlayView gestureOverlayView = (GestureOverlayView) findViewById(R.id.gOverlay);
gestureOverlayView.addOnGesturePerformedListener(gestureListener);
这是gestureListener
:
private OnGesturePerformedListener gestureListener = new OnGesturePerformedListener() {
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
if (predictions.size() > 1) {
for(Prediction prediction: predictions){
Log.d("Test", "Looking for gestures");
}
}
}
};
就这样。在墙上,我在不同的 Activity 中尝试了这个源代码,它没有 ScrollView 并且工作正常。
最后,我不确定是不是关于 GestureDetector,所以这就是应用程序使用检测器的方式:
public boolean dispatchTouchEvent(MotionEvent ev) {
if (detector != null) {
if (detector.onTouchEvent(ev)) {
return true;
}
}
return super.dispatchTouchEvent(ev);
}
我的SwipeDetector
:
private class SwipeDetector extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
public boolean onFling(android.view.MotionEvent e1, android.view.MotionEvent e2, float velocityX, float velocityY) {
if( Math.abs( e1.getY() - e2.getY() ) > SWIPE_MAX_OFF_PATH ) { return false; }
if( e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs( velocityX ) > SWIPE_THRESHOLD_VELOCITY ) { return false; }
if( e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs( velocityX ) > SWIPE_THRESHOLD_VELOCITY ) { filterButton.performClick();return true; }
return false;
}
}
我的方法有什么问题?