I am a beginner in android, and I was going through thenewboston tutorials. I came across this code for gesture recognition.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMessage = (TextView) findViewById(R.id.myMessage);
this.gestureDetector = new GestureDetectorCompat(this, this);
gestureDetector.setOnDoubleTapListener(this);
}
I looked up the documentation for GestureDetectorCompat
, and could see that the constructor used was GestureDetectorCompat(Context context, GestureDetector.OnGestureListener listener)
However, I couldn't understand how new GestureDetectorCompat(this, this);
will create this object. What is the (this, this)
referring to? Where is it getting from? Is there another way to create this object that could help me understand this?
The first parameter this
is the context of your current activity. (SO question about the idea of context What is 'Context' on Android?) The second one is again this
, because your activity should implement implements OnGestureListener, OnDoubleTapListener
and that's why you could write instead of
... new GestureDetector(this, new OnGestureListener() {...} );
//and then
gestureDetector.setOnDoubleTapListener(new OnDoubleTapListener() {...});
just
... new GestureDetectorCompat(this, this);
//and
gestureDetector.setOnDoubleTapListener(this);
If you do not want to use the construction you posted (with this
parameters), than you could easily use the full construction like this for example (just an example of the full use):
gestureDetector.setOnDoubleTapListener(new OnDoubleTapListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
mapView.getController().zoomInFixing((int) e.getX(), (int) e.getY());
return false;
}
//you could override more methods here if you want
}