3

这个问题已经被问过好几次了,我已经彻底阅读并遵循了答案/评论。结果,我能够生成下面的代码,但代码仅响应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 GoogleAndroidMapsActivityas

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()在我的日志中获得“向下”、“显示按下”和“长按”后,单击/双击无效。请帮忙。

4

2 回答 2

0

我有同样的问题。具体来说,我想覆盖地图上的默认双击(SupportMapFragment)。我解决它的方法(它实际上是一种解决方法)是:
我创建了一个框架布局,它填充了与我的地图片段完全相同的区域,但它是在那之后完全渲染的。

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<FrameLayout
    android:id="@+id/fragment_wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

然后我在“覆盖”fragment_wrapper 中添加了一个手势监听器,我能够捕捉到双击:

GestureDetector gestureDetector = new GestureDetector(this, new DoubleTapDetector());
fragmentWrapper.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v,MotionEvent event){
        gestureDetector.onTouchEvent(event);
        return true;
    }
});

DoubleTapDetector 类是:

public class DoubleTapDetector extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.d("DoubleTapDetector", "Double Tap!");
                //DO ACTIONS HERE
        return true;
    }
}
于 2013-02-13T17:41:59.337 回答
0

我是这样做的,只需将此括号放入扩展 MapView 类的类主体中

{
 final GestureDetector gd = new GestureDetector(new SimpleOnGestureListener() {
  @Override
  public boolean onDoubleTap(MotionEvent e) {
    getController().zoomInFixing((int) e.getX(), (int) e.getY());
    return true;
  }
});

this.setOnTouchListener(new OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    return gd.onTouchEvent(event);
  }
});
}
于 2013-01-22T21:55:01.250 回答