As I mentioned on title, app has a ScrollView
and a GestureDetector
too. Outside of ScrollView's touch events, GestureDetector handling swipe actions like left to right and right to left. They're all working well.
Now I want to add a GestureLibrary
-I mean raw- to Activity. I've looked different sources and somehow added properly. Simply, layout looking like this:
<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>
It's drawing as I wanted (the yellow line) but it's not triggering any methods. Here how I implemented OnGesturePerformedListener
:
/*
* Gestures
*/
gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gLibrary.load()) { finish(); }
GestureOverlayView gestureOverlayView = (GestureOverlayView) findViewById(R.id.gOverlay);
gestureOverlayView.addOnGesturePerformedListener(gestureListener);
And here is 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");
}
}
}
};
That's all. By the wall, I tried this source code with in different Activity which has no ScrollView and working fine.
Finally, I'm not sure is it about GestureDetector, so that's how app using detector:
public boolean dispatchTouchEvent(MotionEvent ev) {
if (detector != null) {
if (detector.onTouchEvent(ev)) {
return true;
}
}
return super.dispatchTouchEvent(ev);
}
And my 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;
}
}
What's wrong with my approach?