learn how to detect Swipe left / right in android

First issue: horizontal swipe is not detected in ScrollView

I implemented onFling using below code:

public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
System.out.print(“onFling is called!\n”);
// return true;
try {
if (Math.abs(e1.getY() – e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() – e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//do your code
BackHandler((Button)findViewById(R.id.btn_back));
return true;
} else if (e2.getX() – e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//left to right flip
ContinueHandler((Button)findViewById(R.id.btn_continue));
return true;
}
} catch (Exception e) {
// nothing
}
return false;
}

I used the code from a post on Stackoverflow( http://stackoverflow.com/questions/937313/android-basic-gesture-detection ). It says it’ll work. but when I put it into my project, I found only left swipe will be detected while right swipe can’t invoke any gesture listener method, including onFling, onTouchEvent, onDown.

But then I found out it’s because I used Scrollview in my layout file. If I remove the scrollview, then both left and right swipe will work. however, scrollview has to be in the layout, so what should I do? I found the solution is to override dispatchTouchEvent method of gestureListener  (http://stackoverflow.com/questions/8326599/horizontal-swipe-not-detected-in-scrollviews-parent)

@Override
public boolean dispatchTouchEvent( MotionEvent ev ) {
// TouchEvent dispatcher.
if( detector != null ) {
if( detector.onTouchEvent( ev ) )
// If the gestureDetector handles the event, a swipe has been
// executed and no more needs to be done.
return true;
}
return super.dispatchTouchEvent( ev );
}

Now swipe works in the presence of scrollview. however, i found another issue.

Problem 2: button & spinner can not be selected.

This problem doesn’t show up when the dispatchTouchEvent() method is not overrided as code above. so how to solve this problem? what I did is to change the last line  “return true” to “return false” in onDown, onTouchEvent and onScroll method in which “return true” is the default last (or only) line code in the method.

when I swipe, I found onDown is first called, then onScroll, last onFling. when nothing is done in those code, we should make them return false. Returning true “means” that the event has been consumed, and is therefore not passed on.

if the event is dealt in the current method, then return true, meaning the event is consumed, so no need to pass it on to child views;
Otherwise, return false so the event can be passed on to child views.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s