I was going through some tutorials for detecting common gesture and saw the code given below. Here, GestureDetectorCompat
Object is made to detect the gestures using the event handler SetOnDoubleTapListener
. The main activity class implements two interfaces from GestureDetector
class - OnGestureListener
and OnDoubleTapListener
. I got few question,
1) How came GestureDetectorCompat
object is used to create the event handler SetOnDoubleTapListener
for the callback methods inside of GestureDetector.OnGestureListener
and GestureDetector.OnDoubleTapListener
interfaces since GestureDetectorCompat
and GestureDetector
are two different class and how is it working fine with it.
2) Is SetOnDoubleTapListener
Event Handler method can be used for all the callback methods inside the GestureDetector.OnGestureListener
and GestureDetector.OnDoubleTapListener
interfaces? If yes why they named it SetOnDoubleTapListener
?
3) Why GesutureDetector
class can't have just one interface consist of all the methods defined inside of GestureDetector.OnGestureListener
and GestureDetector.OnDoubleTapListener
interfaces.
4) Whats the difference between GestureDetector
and GestureDetectorCompat
? Why GestureDetectorCompat
class don't have any interface defined.
Code :
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector;
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the gesture detector with the
// application context and an implementation of
// GestureDetector.OnGestureListener
mDetector = new GestureDetectorCompat(this,this);
// Set the gesture detector as the double tap
// listener.
mDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// Be sure to call the superclass implementation
return super.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent event) {
Log.d(DEBUG_TAG,"onDown: " + event.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
return true;
}
@Override
public void onLongPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
return true;
}
@Override
public void onShowPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
return true;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return true;
}
}