private GestureDetector mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGestureDetector = new GestureDetector(this, new LearnGestureListener());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event))
return true;
else
return false;
}
class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onSingleTapUp(MotionEvent ev) {
Log.d("onSingleTapUp",ev.toString());
return true;
}
@Override
public void onShowPress(MotionEvent ev) {
Log.d("onShowPress",ev.toString());
}
@Override
public void onLongPress(MotionEvent ev) {
Log.d("onLongPress",ev.toString());
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("onScroll",e1.toString());
return true;
}
@Override
public boolean onDown(MotionEvent ev) {
Log.d("onDownd",ev.toString());
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("d",e1.toString());
Log.d("e2",e2.toString());
return true;
}
}Explanation
First we create a GestureDetector instance
private GestureDetector mGestureDetector;
Then we create a GestureListener for us to listen
class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{ ...... }
After creating those 2, we bind our listener to our GestureDetector instance
mGestureDetector = new GestureDetector(this, new LearnGestureListener());
Lastly we need to know where the event listener will be trigger, that is where onTouchEvent comes in
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) .....
}
** The function on our LearnGestureListener class have logical name except onFling which means on swipe.
Reference
Code Shogun
Calandar Android App
Update History
Jan 17, 2012 - Visual Update
4 comments:
Hello,
thanks this works perfectly. But is there a way to extend the usual onTouchListener instead of overriding it? I want to extend a webview by longPress and doubleTap but keep the usual behavior of flinging and handling links. Thanks in advance.
Jan
Im not sure if i quite understand your requirement/s but you could refer to this http://www.google.com/codesearch/p?hl=en&sa=N&cd=2&ct=rc#HqP5OxuOo3c/src/codefidence/droidpal/Droidpal.java&q=%22extends%20webview%22%20touch%20lang:java and see if its the one you need. Thanks
Thanks for the link but in this example the onTouch method is overridden as well. Probably I didn't express myself exactly enough.
I mean I want to retain the usual behavior of the webview. So it should keep on handling the onFling-, the onSingleTap-Events, etc. But the onLongPress- and the onDoubleTap-Events that are also provided by GestureDetector.OnGestureListener aren´t handled originally. So I want to implement them without having to inplement the other Events again. That's what i meant with "extend the onTouchListener". Is there a way to reach this? Thanks
not really a big fan of OOP, the tutorial above is extending a class and does it mean its okay to override the function coz the GestureDetector does have a onLongPress and onDoubleTap, you just need to enable the longPress and pass a doubleTap listener, you could refer to http://developer.android.com/reference/android/view/GestureDetector.html#setIsLongpressEnabled%28boolean%29 then if the listeners dont work for this two you could try adding
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return mGestureDetector.onTouchEvent(ev);
}
Post a Comment