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

时间:2013-11-18 14:25:37

标签: android popup android-view

我在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;
            }
        });

1 个答案:

答案 0 :(得分:0)

你有没有试过像

这样的东西
if(popupView == null) {
   // show popup
} else {
   // do nothing
}