首页 > 解决方案 > 触摸外部时弹出窗口不隐藏

问题描述

在空白处点击时我需要关闭我的弹出窗口,但是当滚动时我需要显示一个弹出窗口。现在当我滚动时我的弹出窗口被隐藏了,请帮我解决这个问题。这是我的代码:

private void showPopup(View view, String text) {
    if (infoPopup == null) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View popupView = layoutInflater.inflate(R.layout.popup, null);
        TextView tvPopupText = popupView.findViewById(R.id.tv_popup);
        tvPopupText.setText(text);
        FrameLayout flBackground = popupView.findViewById(R.id.fl_bg);
        flBackground.setBackground(new BubbleDrawable(getContext(), R.color.azure, 16, 16, 8));
        infoPopup = new PopupWindow(popupView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        infoPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // infoPopup.setOutsideTouchable(true);
        infoPopup.showAsDropDown(view);
        infoPopup.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismissInfoPopup();
                    return true;
                }
                return false;
            }
        });
    } else {
        dismissInfoPopup();
    }
}

private void dismissInfoPopup() {
    if (infoPopup != null) {
        infoPopup.dismiss();
    }
    infoPopup = null;
}

现在,当我滚动时会显示弹出窗口,但是当我点击外部弹出窗口时不会隐藏。

标签: androidscrollpopupwindow

解决方案


在弹出显示后添加以下行

infoPopup.setBackgroundDrawable(new BitmapDrawable());
infoPopup.setOutsideTouchable(true);

编辑

infoPopup.setFocusable(true);

推荐阅读