[android] - move touch handling of MarkerViews back to View#setOnClickListener. Workaround panning issue by dispatching touch events to the parent ViewGroup. (#8272)

This commit is contained in:
Tobrun
2017-03-09 16:59:10 -08:00
committed by GitHub
parent b8283e838e
commit 1693b389d7
5 changed files with 53 additions and 60 deletions

View File

@@ -0,0 +1,30 @@
package com.mapbox.mapboxsdk.annotations;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
/**
* ViewGroup that dispatches TouchEvents to the parent ViewGroup.
* <p>
* This allows to dispatch touch events that occur on MarkerView to MapView.
* https://github.com/mapbox/mapbox-gl-native/issues/5388
* </p>
*/
public class MarkerViewContainer extends FrameLayout {
public MarkerViewContainer(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final boolean childResult = super.dispatchTouchEvent(ev);
if (childResult) {
((ViewGroup) getParent()).onTouchEvent(ev);
}
return childResult;
}
}

View File

@@ -503,6 +503,21 @@ public class MarkerViewManager implements MapView.OnMapChangedListener {
}
}
adaptedView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
boolean clickHandled = false;
if (onMarkerViewClickListener != null) {
clickHandled = onMarkerViewClickListener.onMarkerClick(marker, v, adapter);
}
if (!clickHandled) {
ensureInfoWindowOffset(marker);
select(marker, v, adapter);
}
}
});
marker.setMapboxMap(mapboxMap);
markerViewMap.put(marker, adaptedView);
if (convertView == null) {
@@ -530,34 +545,6 @@ public class MarkerViewManager implements MapView.OnMapChangedListener {
updateMarkerViewsPosition();
}
/**
* When the provided {@link MarkerView} is clicked on by a user, we check if a custom click
* event has been created and if not, display a {@link InfoWindow}.
*
* @param markerView that the click event occurred.
*/
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 true;
}
if (onMarkerViewClickListener != null) {
clickHandled = onMarkerViewClickListener.onMarkerClick(markerView, view, adapter);
}
if (!clickHandled) {
ensureInfoWindowOffset(markerView);
select(markerView, view, adapter);
}
return clickHandled;
}
/**
* Handles the {@link MarkerView}'s info window offset.
*

View File

@@ -10,6 +10,7 @@ import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerView;
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
@@ -256,7 +257,11 @@ class AnnotationManager {
private MarkerView prepareViewMarker(BaseMarkerViewOptions markerViewOptions) {
MarkerView marker = markerViewOptions.getMarker();
iconManager.loadIconForMarkerView(marker);
Icon icon = markerViewOptions.getIcon();
if (icon == null) {
icon = IconFactory.getInstance(mapView.getContext()).defaultMarkerView();
}
marker.setIcon(icon);
return marker;
}
@@ -660,24 +665,16 @@ class AnnotationManager {
if (annotation.getId() == newSelectedMarkerId) {
Marker marker = (Marker) annotation;
if (marker instanceof MarkerView) {
handledDefaultClick = markerViewManager.onClickMarkerView((MarkerView) marker);
} else {
if (!(marker instanceof MarkerView)) {
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;
}
}

View File

@@ -71,27 +71,6 @@ class IconManager {
return icon;
}
Icon loadIconForMarkerView(MarkerView marker) {
Icon icon = marker.getIcon();
int iconSize = icons.size() + 1;
if (icon == null) {
icon = IconFactory.getInstance(nativeMapView.getContext()).defaultMarkerView();
marker.setIcon(icon);
}
Bitmap bitmap = icon.getBitmap();
averageIconHeight = averageIconHeight + (bitmap.getHeight() - averageIconHeight) / iconSize;
averageIconWidth = averageIconWidth + (bitmap.getWidth() - averageIconWidth) / iconSize;
if (!icons.contains(icon)) {
icons.add(icon);
} else {
Icon oldIcon = icons.get(icons.indexOf(icon));
if (!oldIcon.getBitmap().sameAs(icon.getBitmap())) {
throw new IconBitmapChangedException();
}
}
return icon;
}
int getTopOffsetPixelsForIcon(Icon icon) {
return (int) (nativeMapView.getTopOffsetPixelsForAnnotationSymbol(icon.getId()) * nativeMapView.getPixelRatio());
}