summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorŁukasz Paczos <lukas.paczos@gmail.com>2018-08-22 18:34:44 +0200
committerŁukasz Paczos <lukasz.paczos@mapbox.com>2018-08-24 17:21:21 +0200
commit0f58d956d7b525f3722f24494784c8b97801105a (patch)
treeb45a93c8257fe3d831e6bba4bda0146f860b72d0
parentf9fd9c0f0086fa2883aa2125c9b95c599c83c4e5 (diff)
downloadqtlocation-mapboxgl-0f58d956d7b525f3722f24494784c8b97801105a.tar.gz
[android] invalidate InfoWindow's y-offset when content is set
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java40
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java2
2 files changed, 37 insertions, 5 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java
index cf42bfe738..f81922097e 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/InfoWindow.java
@@ -2,11 +2,13 @@ package com.mapbox.mapboxsdk.annotations;
import android.content.res.Resources;
import android.graphics.PointF;
+import android.os.Build;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.view.ViewTreeObserver;
import android.widget.TextView;
import com.mapbox.mapboxsdk.R;
@@ -38,6 +40,7 @@ public class InfoWindow {
private float markerHeightOffset;
private float markerWidthOffset;
private float viewWidthOffset;
+ private float viewHeightOffset;
private PointF coordinates;
private boolean isVisible;
@@ -124,8 +127,7 @@ public class InfoWindow {
if (view != null && mapboxMap != null) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
- // Calculate y-offset for update method
- markerHeightOffset = -view.getMeasuredHeight() + offsetY;
+ markerHeightOffset = offsetY;
markerWidthOffset = -offsetX;
// Calculate default Android x,y coordinate
@@ -194,8 +196,10 @@ public class InfoWindow {
view.setX(x);
view.setY(y);
- // Calculate x-offset for update method
+ // Calculate x-offset and y-offset for update method
viewWidthOffset = x - coordinates.x - offsetX;
+ viewHeightOffset = -view.getMeasuredHeight() + offsetY;
+
close(); // if it was already opened
mapView.addView(view, lp);
@@ -288,10 +292,38 @@ public class InfoWindow {
} else {
view.setX(coordinates.x - (view.getMeasuredWidth() / 2) - markerWidthOffset);
}
- view.setY(coordinates.y + markerHeightOffset);
+ view.setY(coordinates.y + viewHeightOffset);
+ }
+ }
+
+ void onContentUpdate() {
+ //recalculate y-offset and update position
+ View view = this.view.get();
+ if (view != null) {
+ ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
+ if (viewTreeObserver.isAlive()) {
+ viewTreeObserver.addOnGlobalLayoutListener(contentUpdateListener);
+ }
}
}
+ private final ViewTreeObserver.OnGlobalLayoutListener contentUpdateListener =
+ new ViewTreeObserver.OnGlobalLayoutListener() {
+ @Override
+ public void onGlobalLayout() {
+ View view = InfoWindow.this.view.get();
+ if (view != null) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
+ } else {
+ view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
+ }
+ viewHeightOffset = -view.getMeasuredHeight() + markerHeightOffset;
+ update();
+ }
+ }
+ };
+
/**
* Retrieve this {@link InfoWindow}'s current view being used.
*
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java
index dad3d61f0c..d0e5777602 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java
@@ -200,7 +200,7 @@ public class Marker extends Annotation {
if (map != null) {
map.updateMarker(this);
}
- infoWindow.update();
+ infoWindow.onContentUpdate();
}
}