Android mapView ItemizedOverlay setFocus无法正常工作
作者:互联网
在ItemizedOverlay上调用setFocus(null)不会“取消聚焦”当前标记.根据文件:
… If the Item is not found, this is a no-op. You can also pass null to remove focus.
这是我的代码:
MapItemizedOverlay
public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
public MapItemizedOverlay(Drawable defaultMarker) {
super(defaultMarker);
}
public void addOverlay(OverlayItem overlay) {
items.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return items.get(i);
}
@Override
public int size() {
return items.size();
}
}
创建地图叠加层和一个标记:
StateListDrawable youIcon = (StateListDrawable)getResources().getDrawable(R.drawable.marker_icon);
int width = youIcon.getIntrinsicWidth();
int height = youIcon.getIntrinsicHeight();
youIcon.setBounds(-13, 0-height, -13+width, 0);
GeoPoint location = new GeoPoint(40800816,-74122009);
MapItemizedOverlay overlay = new MapItemizedOverlay(youIcon);
OverlayItem item = new OverlayItem(location, "title", "snippet");
overlay.addOverlay(item);
mapView.getOverlays().add(overlay);
R.drawable.marker_icon定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/marker_selected" />
<item android:state_selected="true" android:drawable="@drawable/marker_selected" />
<item android:drawable="@drawable/marker_normal" />
</selector>
现在,为了测试setFocus()行为,我将按钮放在活动窗口上,使用以下onClick监听器:
Button focusBtn = (Button)findViewById(R.id.focusbtn);
focusBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for(Overlay ov : mapView.getOverlays())
{
if(ov.getClass().getSimpleName().equals("MapItemizedOverlay") == true)
{
MapItemizedOverlay miv = (MapItemizedOverlay)ov;
if(miv.getFocus() == null)
miv.setFocus(miv.getItem(0));
else
miv.setFocus(null);
break;
}
}
mapView.invalidate();
}
});
预期的行为是:单击按钮切换标记选择.
它只工作一次 – 第一次单击它选择标记,再次单击它不会取消选择标记.最奇怪的是,在调用setFocus(null)之后,getFocus()也返回null – 就像overlay没有焦点项(我调试它).但即使在调用mapView.invalidate()之后,标记仍然以“选定”(聚焦)状态绘制.
解决方法:
正如Rpond在对我的问题的评论中所说,它看起来像API中的一个漏洞.
与此同时,我自己解决了这个问题.以下是变通方法代码.您需要扩展OverlayItem类并检查overlay.getFocus()返回的内容.
public class MapOverlayItem extends OverlayItem {
MapItemizedOverlay overlay = null;
public MapOverlayItem(GeoPoint point, MapItemizedOverlay ov)
{
super(point, null, null);
this.overlay = ov;
}
public MapOverlayItem(GeoPoint point, String title, String snippet) {
super(point, title, snippet);
}
@Override
public Drawable getMarker(int stateBitset) {
Drawable icon = overlay.getDefaultMarker();
if(stateBitset == 0)
return icon;
OverlayItem focusedItem = overlay.getFocus();
if(focusedItem == null) {
OverlayItem.setState(icon, 0);
return icon;
}
if(focusedItem.equals(this) == true)
OverlayItem.setState(icon, stateBitset);
else
OverlayItem.setState(icon, 0);
return icon;
}
}
标签:android,google-maps,setfocus,itemizedoverlay 来源: https://codeday.me/bug/20190726/1546298.html