问题
It´s possible that overlays on a map could receive focus from DPAD/Tab?
I have two fragments, a listview and mapview, I want to get focus from the drawable of the overlay, but I´m not sure if it´s possible...
回答1:
Yes, you can move from one overlay item on MapView to other but there are few things which you should consider.
- If you want your MapView to steer according to the Dpad directions while pressing up/down/left on Dpad, then your map will go up/down/left direction showing the map and you wont able to Dpad on overlay items since MapView is having the focus.
- But if you want overlay items to be focused, then you have to manually define which overlay item it should focus on which D-Pad direction using setFocus, nextFocus and getFocus methods of ItemizedOverlay class.
- Also you said you have listview and MapView in your activity and in order to get the focus back to listview or any other view which is outside MapView will also have to be done programmatically and it could be little tricky.
You can use StateListDrawable to define the different states on overlaid drawable for focus, pressed and default state.
Hope this answers your query.
回答2:
I created a sample activity below. Most of the code comes from the MapView tutorial found here: http://developer.android.com/resources/tutorials/views/hello-mapview.html
The 'focus code' is in the onKeyDown() method. When TAB is pressed, focus is shifted to the next overlay. When ENTER is pressed, it shows a Toast, but that's where you can display your content.
The setFocus() method was found in the documentation for ItemizedOverlay found here: https://developers.google.com/maps/documentation/android/reference/
Hope this works.
public class OverlayFocusExampleActivity extends MapActivity {
private HelloItemizedOverlay itemizedoverlay;
private MapView mapView;
private MapController mapController;
private int currentOverlayIndex;
/*
* This entire method comes from the MapView tutorial.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
List<Overlay> mapOverlays = mapView.getOverlays();
// overlay_draw is a selector that specifies a different image for state_focused
Drawable drawable = this.getResources().getDrawable(R.drawable.overlay_draw);
itemizedoverlay = new HelloItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000, -99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
itemizedoverlay.addOverlay(overlayitem2);
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
OverlayItem overlay;
switch (keyCode) {
case KeyEvent.KEYCODE_TAB:
// Retrieve next overlay
currentOverlayIndex = (currentOverlayIndex + 1) % itemizedoverlay.size();
overlay = itemizedoverlay.getOverlayItem(currentOverlayIndex);
itemizedoverlay.setFocus(overlay);
// Since setFocus() doesn't center the map, we do it ourselves
mapController.animateTo(overlay.getPoint());
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
overlay = itemizedoverlay.getFocus();
if (overlay != null) {
// Perform associated action
// Stub
Toast.makeText(this, overlay.getSnippet(), Toast.LENGTH_SHORT).show();
return true;
}
default:
return false;
}
}
/*
* This entire class comes from the MapView tutorial except getOverlayItem().
*/
private class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
/*
* Not in MapView tutorial. Added for focusability.
*/
public OverlayItem getOverlayItem(int index) {
return mOverlays.get(index);
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
}
来源:https://stackoverflow.com/questions/11052572/navigation-between-overlays