这个问题已经被问过好几次了,我已经彻底阅读并遵循了答案/评论。结果,我能够生成下面的代码,但代码仅响应onDown()
, onShowPress()
, onLongPress()
。以下是我打算启用以捕获双击或双击的唯一类。
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class GoogleAndroidMapsActivity extends MapActivity implements OnGestureListener, OnDoubleTapListener {
private GestureDetector gestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//To allow zooming along with panning around, enable it
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(this);
mapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("GMaps", "Double Tap");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.d("GMaps", "Double Tap Event");
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("GMaps", "Single Tap Confirmed");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("GMaps", "Down");
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("GMaps", "Fling");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d("GMaps", "Long Press");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("GMaps", "Scroll");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.d("GMaps", "Show Press");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("GMaps", "Single Tap Up");
return false;
}
}
但我不明白为什么onDoubleTap()
,onDoubleTapEvent()
也从来没有被叫过。事实上,唯一被调用的是onDown()
, onShowPress()
, onLongPress()
。
我什至尝试如下所示扩展 MapView,然后在我的主类中使用,GoogleAndroidMapsActivity.java
但无济于事。我main.xml
相应地改变以反映AdvMapView.java
。
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import com.google.android.maps.MapView;
public class AdvMapView extends MapView implements OnGestureListener, OnDoubleTapListener{
private GestureDetector gestureDetector;
public AdvMapView(Context context, AttributeSet attrSet) {
super(context, attrSet);
gestureDetector = new GestureDetector(context, this);
gestureDetector.setOnDoubleTapListener(this);
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("GMaps", "Double Tap");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.d("GMaps", "Double Tap Event");
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("GMaps", "Single Tap Confirmed");
return false;
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("GMaps", "Down");
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("GMaps", "Fling");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d("GMaps", "Long Press");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("GMaps", "Scroll");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.d("GMaps", "Show Press");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("GMaps", "Single Tap Up");
return false;
}
}
然后使用 in GoogleAndroidMapsActivity
as
import android.os.Bundle;
import android.view.GestureDetector;
import com.google.android.maps.MapActivity;
public class GoogleAndroidMapsActivity extends MapActivity{
private GestureDetector gestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//To allow zooming along with panning around, enable it
AdvMapView mapView = (AdvMapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
此外,onDown()
,仅在我第一次单击/双击时被调用onShowPress()
。onLongPress()
在我的日志中获得“向下”、“显示按下”和“长按”后,单击/双击无效。请帮忙。