GestureDetector and gesturedetector
Because it is not easy to determine the real idea of user gestures in onTouchEvent, Android provides the GestureDetector detector to help us identify gestures. With the help of GestureDetector, you can identify several commonly used gesture events in most cases, such as click, long press, and flip pages. The following describes how to use GestureDetector:
Constructor: GestureDetector (Context context, OnGestureListener listener)
Listener Class Name: OnGestureListener
Set the listener method, first register the touch listener for the specified control, and then the GestureDetector takes over the touch event in the onTouch method:
private ScrollTextView tv_rough; private GestureDetector mGesture; tv_rough = (ScrollTextView) findViewById(R.id.tv_rough); tv_rough.setOnTouchListener(this); mGesture = new GestureDetector(this, this); @Override public boolean onTouch(View v, MotionEvent event) { return mGesture.onTouchEvent(event); }
In addition, you can also override the onTouchEvent method in the current view or Activity. In this method, GestureDetector takes over the touch event.
Method to override the listener:
OnDown: called when the user presses
OnShowPress: called when pressed but not slide or released, usually used for highlighting when pressed
OnSingleTapUp: called when a user clicks it and then plays it again. It is usually used to click an event.
OnScroll: called during user sliding
OnLongPress
OnFling: called when a user quickly fetches a distance, usually used for page turning events