美文网首页
PopupWindow踩坑

PopupWindow踩坑

作者: Brian512 | 来源:发表于2018-12-21 18:00 被阅读11次
    public void showAtLocation(View parent, int gravity, int x, int y) {
            mParentRootView = new WeakReference<>(parent.getRootView());
            showAtLocation(parent.getWindowToken(), gravity, x, y);
    }
    public void showAtLocation(IBinder token, int gravity, int x, int y)
    

    复用Window,需要注意与dialog的层级

    如果是使用dialog的Token,则PopupWindow不会超出dialog的范围

    IBinder windowToken = BaseActivity.topActivity().getWindow().getDecorView().getWindowToken();
    callMethod(PopupWindow.class, popupWindow, "showAtLocation", windowToken, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
    
      public static Object callMethod(Class<?> cls, Object obj, String name, Object... args) {
            try {
                if (args != null && args.length > 0) {
                    Class<?> parameterTypes[] = new Class<?>[args.length];
                    Object objs[] = new Object[args.length];
    
                    for (int i = 0; i < args.length; ++i) {
                        if (args[i] instanceof Arg) {
                            Arg arg = (Arg) args[i];
                            parameterTypes[i] = arg.type;
                            objs[i] = arg.value;
                        } else {
                            if (args[i] instanceof Integer) {
                                parameterTypes[i] = int.class;
                            } else if (args[i] instanceof Long) {
                                parameterTypes[i] = long.class;
                            } else if (args[i] instanceof Boolean) {
                                parameterTypes[i] = boolean.class;
                            } else if (args[i] instanceof Byte) {
                                parameterTypes[i] = byte.class;
                            } else if (args[i] instanceof Double) {
                                parameterTypes[i] = double.class;
                            } else if (args[i] instanceof Float) {
                                parameterTypes[i] = float.class;
                            } else {
                                parameterTypes[i] = args[i].getClass();
                            }
    
                            objs[i] = args[i];
                        }
                    }
    
                    if (name == null || name.length() <= 0) {
                        Constructor method = cls.getConstructor(parameterTypes);
                        method.setAccessible(true);
                        return method.newInstance(objs);
                    }
    
                    try {
                        Method method = cls.getDeclaredMethod(name, parameterTypes);
                        method.setAccessible(true);
                        return method.invoke(obj, objs);
                    } catch (NoSuchMethodException e) {
                        Method[] method = cls.getDeclaredMethods();
                        for (int i = 0; i < method.length; ++i) {
                            if (method[i].getName().equals(name)) {
                                method[i].setAccessible(true);
                                return method[i].invoke(obj, objs);
                            }
                        }
                    }
                } else {
                    if (name == null || name.length() <= 0) {
                        Constructor method = cls.getConstructor();
                        method.setAccessible(true);
                        return method.newInstance();
                    }
    
                    try {
                        Method method = cls.getDeclaredMethod(name);
                        method.setAccessible(true);
                        return method.invoke(obj);
                    } catch (NoSuchMethodException e) {
                        Method[] method = cls.getDeclaredMethods();
                        for (int i = 0; i < method.length; ++i) {
                            if (method[i].getName().equals(name)) {
                                method[i].setAccessible(true);
                                return method[i].invoke(obj);
                            }
                        }
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
    
            return null;
        }
    

    相关文章

      网友评论

          本文标题:PopupWindow踩坑

          本文链接:https://www.haomeiwen.com/subject/ytvdkqtx.html