[android] - refactor marker selection and listener invocation, reformatted code (#7304)

This commit is contained in:
Tobrun
2016-12-08 14:37:15 +01:00
committed by GitHub
parent 7cf10ee524
commit 1d93ffc5eb
2 changed files with 31 additions and 25 deletions

View File

@@ -9,12 +9,10 @@ import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.utils.AnimatorUtils;
@@ -34,7 +32,7 @@ public class MarkerViewManager {
private final ViewGroup markerViewContainer;
private final Map<MarkerView, View> markerViewMap = new HashMap<>();
private final List<MapboxMap.MarkerViewAdapter> markerViewAdapters = new ArrayList<>();
private final List<MapboxMap.MarkerViewAdapter> markerViewAdapters = new ArrayList<>();
// TODO refactor MapboxMap out for Projection and Transform
// Requires removing MapboxMap from Annotations by using Peer model from #6912
@@ -55,7 +53,7 @@ public class MarkerViewManager {
// TODO refactor MapboxMap out for Projection and Transform
// Requires removing MapboxMap from Annotations by using Peer model from #6912
public void bind(MapboxMap mapboxMap){
public void bind(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
}
@@ -487,14 +485,14 @@ public class MarkerViewManager {
*
* @param markerView that the click event occurred.
*/
public void onClickMarkerView(MarkerView markerView) {
public boolean onClickMarkerView(MarkerView markerView) {
boolean clickHandled = false;
MapboxMap.MarkerViewAdapter adapter = getViewAdapter(markerView);
View view = getView(markerView);
if (adapter == null || view == null) {
// not a valid state
return;
return true;
}
if (onMarkerViewClickListener != null) {
@@ -505,6 +503,8 @@ public class MarkerViewManager {
ensureInfoWindowOffset(markerView);
select(markerView, view, adapter);
}
return clickHandled;
}
/**

View File

@@ -5,7 +5,6 @@ import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.LongSparseArray;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
@@ -301,23 +300,16 @@ class AnnotationManager implements MapView.OnMapChangedListener {
deselectMarkers();
}
boolean handledDefaultClick = false;
if (onMarkerClickListener != null) {
// end developer has provided a custom click listener
handledDefaultClick = onMarkerClickListener.onMarkerClick(marker);
if (marker instanceof MarkerView) {
markerViewManager.select((MarkerView) marker, false);
markerViewManager.ensureInfoWindowOffset((MarkerView) marker);
}
if (!handledDefaultClick) {
if (marker instanceof MarkerView) {
markerViewManager.select((MarkerView) marker, false);
markerViewManager.ensureInfoWindowOffset((MarkerView) marker);
}
if (infoWindowManager.isInfoWindowValidForMarker(marker) || infoWindowManager.getInfoWindowAdapter() != null) {
infoWindowManager.add(marker.showInfoWindow(mapboxMap, mapView));
}
if (infoWindowManager.isInfoWindowValidForMarker(marker) || infoWindowManager.getInfoWindowAdapter() != null) {
infoWindowManager.add(marker.showInfoWindow(mapboxMap, mapView));
}
// only add to selected markers if user didn't handle the click event themselves #3176
selectedMarkers.add(marker);
}
@@ -655,13 +647,27 @@ class AnnotationManager implements MapView.OnMapChangedListener {
Annotation annotation = annotations.get(i);
if (annotation instanceof Marker) {
if (annotation.getId() == newSelectedMarkerId) {
if (selectedMarkers.isEmpty() || !selectedMarkers.contains(annotation)) {
if (!(annotation instanceof MarkerView)) {
selectMarker((Marker) annotation);
} else {
markerViewManager.onClickMarkerView((MarkerView) annotation);
Marker marker = (Marker) annotation;
boolean handledDefaultClick = false;
if (marker instanceof MarkerView) {
handledDefaultClick = markerViewManager.onClickMarkerView((MarkerView) marker);
} else {
if (onMarkerClickListener != null) {
// end developer has provided a custom click listener
handledDefaultClick = onMarkerClickListener.onMarkerClick(marker);
}
}
if (annotation instanceof MarkerView) {
markerViewManager.onClickMarkerView((MarkerView) annotation);
} else {
if (!handledDefaultClick) {
// only select marker if user didn't handle the click event themselves
selectMarker(marker);
}
}
return true;
}
}