我有一个Osmdroid MapView。即使我已经设置
mapView.setClickable(false);
mapView.setFocusable(false);
地图仍然可以移动。有没有一种简单的方法可以禁用与地图视图的所有交互?
一个简单的解决方案是像@Schrieveslaach 一样使用mapView:
mapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
我找到了解决方案。您需要通过设置一个OnTouchListener
. 例如,
public class MapViewLayout extends RelativeLayout {
private MapView mapView;
/**
* @see #setDetachedMode(boolean)
*/
private boolean detachedMode;
// implement initialization of your layout...
private void setUpMapView() {
mapView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (detachedMode) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// if you want to fire another event
}
// Is detached mode is active all other touch handler
// should not be invoked, so just return true
return true;
}
return false;
}
});
}
/**
* Sets the detached mode. In detached mode no interactions will be passed to the map, the map
* will be static (no movement, no zooming, etc).
*
* @param detachedMode
*/
public void setDetachedMode(boolean detachedMode) {
this.detachedMode = detachedMode;
}
}
你可以试试:
mapView.setEnabled(false);
这应该禁用与地图视图的所有交互
我的解决方案类似于@schrieveslaach 和@sagix,但我只是扩展了基MapView
类并添加了新功能:
class DisabledMapView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : MapView(context, attrs) {
private var isUserInteractionEnabled = true
override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
if (isUserInteractionEnabled.not()) {
return false
}
return super.dispatchTouchEvent(event)
}
fun setUserInteractionEnabled(isUserInteractionEnabled: Boolean) {
this.isUserInteractionEnabled = isUserInteractionEnabled
}
}