繁体   English   中英

Android:可以多次创建弹出窗口

[英]Android: Popup window can be created multiple times

我在Android应用程序中有一个图像视图,单击该图像时会显示一个包含信息的弹出窗口。 问题是,当多次单击图像视图时,会在旧视图上创建一个新的弹出窗口,并且它们是相同的……我一次只需要一个弹出窗口。 这是我的代码

public void showPopup(View anchorView) {
        LayoutInflater li = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = li.inflate(R.layout.popup, null);
        final PopupWindow popup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        popup.setFocusable(true);
        popup.setBackgroundDrawable(new ColorDrawable());
        int location[] = new int[2];
        anchorView.getLocationOnScreen(location);
        popup.showAtLocation(anchorView, Gravity.NO_GRAVITY, location[0], location[1] + anchorView.getHeight());
        popupView.setOnTouchListener(new OnTouchListener() {
            /** Popup Window Drag Event */

            int dx = 0;
            int dy = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    dx = (int) event.getX();
                    dy = (int) event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int x = (int) event.getRawX();
                    int y = (int) event.getRawY();
                    int left = (x - dx);
                    int top = (y - dy);
                    popup.update(left, top, -1, -1);
                    break;
                }
                return true;
            }
        });

你尝试过类似的东西吗

if(popupView == null) {
   // show popup
} else {
   // do nothing
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM