正如我在標題中提到的,應用程序也有ScrollView
和GestureDetector
。在ScrollView的觸摸事件之外,GestureDetector處理滑動操作,如從左到右和從右到左。他們都很好。在ScrollView中使用GestureLibrary
現在我想添加一個GestureLibrary
- 我的意思是raw-到Activity。我看了不同的來源,並以某種方式正確添加。簡單地說,佈局看起來像這樣:
<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;
}
}
這有什麼錯我的做法?
什麼是「探測器」?它是SwipeDetector對象嗎? –
@RahulHawge,「探測器」是GestureDetector對象。 – strizzwald