Explorar el Código

0.0.48.beta4
- PopNotification 默认支持了向上滑动关闭操作,如不需要可通过 `.setSlideToClose(false)` 关闭;
- PopMenu、GuideDialog、CustomDialog 的基于 baseView 显示现在将实时跟踪 baseView 的位置进行显示;
- bug 修复;

myzcxhh@live.cn hace 2 años
padre
commit
aa196448d7

+ 38 - 10
DialogX/src/main/java/com/kongzue/dialogx/dialogs/CustomDialog.java

@@ -155,6 +155,7 @@ public class CustomDialog extends BaseDialog {
         return this;
     }
 
+    private ViewTreeObserver viewTreeObserver;
     private ViewTreeObserver.OnDrawListener baseViewDrawListener;
 
     public class DialogImpl implements DialogConvertViewInterface {
@@ -299,15 +300,24 @@ public class CustomDialog extends BaseDialog {
                         }
                     };
 
-                    boxCustom.getViewTreeObserver().addOnDrawListener(baseViewDrawListener = new ViewTreeObserver.OnDrawListener() {
+                    viewTreeObserver = boxCustom.getViewTreeObserver();
+                    viewTreeObserver.addOnDrawListener(baseViewDrawListener = new ViewTreeObserver.OnDrawListener() {
                         @Override
                         public void onDraw() {
                             int[] baseViewLocCache = new int[2];
-                            baseView.getLocationOnScreen(baseViewLocCache);
-                            if (baseViewLoc == null || baseViewLocCache[0] != baseViewLoc[0] || baseViewLocCache[1] != baseViewLoc[1]) {
-                                baseViewLoc = baseViewLocCache;
-                                if (getDialogImpl() != null) {
-                                    onLayoutChangeRunnable.run();
+                            if (baseView != null) {
+                                baseView.getLocationOnScreen(baseViewLocCache);
+                                if (baseViewLoc == null || baseViewLocCache[0] != baseViewLoc[0] || baseViewLocCache[1] != baseViewLoc[1]) {
+                                    baseViewLoc = baseViewLocCache;
+                                    if (getDialogImpl() != null) {
+                                        onLayoutChangeRunnable.run();
+                                    }
+                                }
+                            } else {
+                                if (viewTreeObserver != null) {
+                                    viewTreeObserver.removeOnDrawListener(this);
+                                    viewTreeObserver = null;
+                                    baseViewDrawListener = null;
                                 }
                             }
                         }
@@ -439,8 +449,16 @@ public class CustomDialog extends BaseDialog {
                                 }
                                 if (value == 0) {
                                     if (boxRoot != null) boxRoot.setVisibility(View.GONE);
-                                    if (boxCustom != null && baseViewDrawListener != null) {
-                                        boxCustom.getViewTreeObserver().removeOnDrawListener(baseViewDrawListener);
+                                    if (baseViewDrawListener != null) {
+                                        if (viewTreeObserver != null) {
+                                            viewTreeObserver.removeOnDrawListener(baseViewDrawListener);
+                                        } else {
+                                            if (boxCustom != null) {
+                                                boxCustom.getViewTreeObserver().removeOnDrawListener(baseViewDrawListener);
+                                            }
+                                        }
+                                        baseViewDrawListener = null;
+                                        viewTreeObserver = null;
                                     }
                                     dismiss(dialogView);
                                 }
@@ -749,8 +767,18 @@ public class CustomDialog extends BaseDialog {
     @Override
     public void restartDialog() {
         if (dialogView != null) {
-            if (getDialogImpl() != null && getDialogImpl().boxCustom != null && baseViewDrawListener != null) {
-                getDialogImpl().boxCustom.getViewTreeObserver().removeOnDrawListener(baseViewDrawListener);
+            if (getDialogImpl() != null && getDialogImpl().boxCustom != null) {
+                if (baseViewDrawListener != null) {
+                    if (viewTreeObserver != null) {
+                        viewTreeObserver.removeOnDrawListener(baseViewDrawListener);
+                    } else {
+                        if (getDialogImpl().boxCustom != null) {
+                            getDialogImpl().boxCustom.getViewTreeObserver().removeOnDrawListener(baseViewDrawListener);
+                        }
+                    }
+                    baseViewDrawListener = null;
+                    viewTreeObserver = null;
+                }
             }
             dismiss(dialogView);
             isShow = false;

+ 46 - 15
DialogX/src/main/java/com/kongzue/dialogx/dialogs/PopMenu.java

@@ -205,6 +205,7 @@ public class PopMenu extends BaseDialog {
         return popMenu;
     }
 
+    private ViewTreeObserver viewTreeObserver;
     private ViewTreeObserver.OnDrawListener baseViewDrawListener;
 
     public PopMenu show() {
@@ -240,20 +241,29 @@ public class PopMenu extends BaseDialog {
         }
         show(dialogView);
         if (baseView != null) {
-            baseView.getViewTreeObserver().addOnDrawListener(baseViewDrawListener = new ViewTreeObserver.OnDrawListener() {
+            viewTreeObserver = baseView.getViewTreeObserver();
+            viewTreeObserver.addOnDrawListener(baseViewDrawListener = new ViewTreeObserver.OnDrawListener() {
                 @Override
                 public void onDraw() {
                     int[] baseViewLocCache = new int[2];
-                    baseView.getLocationOnScreen(baseViewLocCache);
-                    if (baseViewLoc == null || baseViewLocCache[0] != baseViewLoc[0] || baseViewLocCache[1] != baseViewLoc[1]) {
-                        baseViewLoc = baseViewLocCache;
-                        if (getDialogImpl() != null) {
-                            if (getDialogImpl().boxBody.getX() < 0 && getDialogImpl().boxBody.getY() < 0) {
-                                setDefaultMenuBodyLoc();
-                            } else {
-                                refreshMenuBodyLoc();
+                    if (baseView != null) {
+                        baseView.getLocationOnScreen(baseViewLocCache);
+                        if (baseViewLoc == null || baseViewLocCache[0] != baseViewLoc[0] || baseViewLocCache[1] != baseViewLoc[1]) {
+                            baseViewLoc = baseViewLocCache;
+                            if (getDialogImpl() != null) {
+                                if (getDialogImpl().boxBody.getX() < 0 && getDialogImpl().boxBody.getY() < 0) {
+                                    setDefaultMenuBodyLoc();
+                                } else {
+                                    refreshMenuBodyLoc();
+                                }
                             }
                         }
+                    } else {
+                        if (viewTreeObserver != null) {
+                            viewTreeObserver.removeOnDrawListener(this);
+                            viewTreeObserver = null;
+                            baseViewDrawListener = null;
+                        }
                     }
                 }
             });
@@ -262,6 +272,9 @@ public class PopMenu extends BaseDialog {
     }
 
     private void setDefaultMenuBodyLoc() {
+        if (getDialogImpl() == null || getDialogImpl().boxRoot == null) {
+            return;
+        }
         int width = PopMenu.this.width == 0 ? baseView.getWidth() : PopMenu.this.width;
         int height = PopMenu.this.height == 0 ? baseView.getHeight() : PopMenu.this.height;
 
@@ -282,6 +295,9 @@ public class PopMenu extends BaseDialog {
     }
 
     private void refreshMenuBodyLoc() {
+        if (getDialogImpl() == null || getDialogImpl().boxRoot == null) {
+            return;
+        }
         MaxRelativeLayout boxBody = getDialogImpl().boxBody;
         DialogXBaseRelativeLayout boxRoot = getDialogImpl().boxRoot;
         //菜单位置计算逻辑
@@ -348,7 +364,7 @@ public class PopMenu extends BaseDialog {
 
             if (calX != 0) boxBody.setX(calX);
             if (calY != 0) boxBody.setY(calY);
-        }else{
+        } else {
             setDefaultMenuBodyLoc();
         }
     }
@@ -553,8 +569,16 @@ public class PopMenu extends BaseDialog {
                                     boxRoot.setBkgAlpha(value);
                                 }
                                 if (value == 0f) {
-                                    if (baseView != null && baseViewDrawListener != null) {
-                                        baseView.getViewTreeObserver().removeOnDrawListener(baseViewDrawListener);
+                                    if (baseViewDrawListener != null) {
+                                        if (viewTreeObserver != null) {
+                                            viewTreeObserver.removeOnDrawListener(baseViewDrawListener);
+                                        } else {
+                                            if (baseView != null) {
+                                                baseView.getViewTreeObserver().removeOnDrawListener(baseViewDrawListener);
+                                            }
+                                        }
+                                        baseViewDrawListener = null;
+                                        viewTreeObserver = null;
                                     }
                                     dismiss(dialogView);
                                 }
@@ -616,7 +640,7 @@ public class PopMenu extends BaseDialog {
 
                                     if (!offScreen) {
                                         float calX = baseViewLoc[0];
-                                        float calY = baseViewLoc[1]+selectItemYDeviation;
+                                        float calY = baseViewLoc[1] + selectItemYDeviation;
 
                                         if (calX < 0) {
                                             calX = 0;
@@ -768,8 +792,15 @@ public class PopMenu extends BaseDialog {
     @Override
     public void restartDialog() {
         if (dialogView != null) {
-            if (baseView != null && baseViewDrawListener != null) {
-                baseView.getViewTreeObserver().removeOnDrawListener(baseViewDrawListener);
+            if (baseViewDrawListener != null) {
+                if (viewTreeObserver != null) {
+                    viewTreeObserver.removeOnDrawListener(baseViewDrawListener);
+                } else {
+                    if (baseView != null) {
+                        baseView.getViewTreeObserver().removeOnDrawListener(baseViewDrawListener);
+                    }
+                }
+                baseViewDrawListener = null;
             }
             dismiss(dialogView);
             isShow = false;

+ 0 - 1
app/src/main/java/com/kongzue/dialogxdemo/activity/MainActivity.java

@@ -428,7 +428,6 @@ public class MainActivity extends BaseActivity {
         btnSelectMenu.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
-                view.animate().y(view.getY()-dip2px(100)).setDuration(5000);
                 PopMenu.show(view, new String[]{"选项1", "选项2", "选项3"})
                         .setOnMenuItemClickListener(new OnMenuItemClickListener<PopMenu>() {
                             @Override

+ 1 - 1
gradle.properties

@@ -19,6 +19,6 @@ android.useAndroidX=true
 # Automatically convert third-party libraries to use AndroidX
 android.enableJetifier=true
 
-BUILD_VERSION=0.0.48.beta3
+BUILD_VERSION=0.0.48.beta4
 BUILD_VERSION_INT=47
 DIALOGX_STYLE_VERSION=5