summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorŁukasz Paczos <lukas.paczos@gmail.com>2018-08-22 18:34:44 +0200
committerŁukasz Paczos <lukas.paczos@gmail.com>2018-08-22 18:36:38 +0200
commit32a156d83f4f23c0ba7132e0e840de0a0719f9ad (patch)
tree049a98f67f0d5bcf7ef6714744005b2b4ca94c25
parent520df7f02049cdbbb9e68041e755d6c3a8d5b21f (diff)
downloadqtlocation-mapboxgl-upstream/12694-info-window-resize-anchor.tar.gz
[android] invalidate InfoWindow's y-offset when content is setupstream/12694-info-window-resize-anchor
-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();
}
}