popupWindow通过按钮显示后,按下时消失,抬起时候又显示的问题

如果出现第一次点击Button会显示,再次点击就消失不了的问题(因为按下的时候popupWindo会先消失,但是抬起来又会显示)

(核心就是设置popupWindo的点击事件拦截监听,这里判断了是否点击到popupWindo的外面并且在指定的控件上面)

解决方案:直接来代码,


设置Pop点击事件拦截监听
public class MainActivity extends AppCompatActivity {
    private MePopupWindow popupWindow;
    private View btn;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAndDismissPop();
            }
        });
    }


    /**
     * 显示pop/隐藏pop
     */
    public void showAndDismissPop() {
        //隐藏pop
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            return;
        }
        //显示pop
        if (popupWindow == null) {//创建新的
            createPopupWindow(popupWindow = new MePopupWindow(this));
        } else if (!popupWindow.isShowing()) {//复用老的
            popupWindow.showAsDropDown(btn);
        }

    }


    /**
     * 创建新的窗体并显示
     */
    public void createPopupWindow(MePopupWindow popupWindow) {
        View root = View.inflate(this, R.layout.pop_filtrate, null);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);//这个是点击外部消失
        popupWindow.setContentView(root);
        setPopTouchInterceptor();
        popupWindow.setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(FrameLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.showAsDropDown(btn);
    }

    /**
     * 设置Pop点击事件拦截监听
     */
    public void setPopTouchInterceptor() {
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.dismiss();
                //如果焦点不在popupWindow                if (event.getAction() == MotionEvent.ACTION_OUTSIDE && !popupWindow.isFocusable()) {
                    //并且点击到了Button上面,那么返回true,不做dismiss的操作,反之返回false
                    return isInChangeImageZone(btn, (int) event.getRawX(), (int) event.getRawY());
                }
                return false;
            }
        });
    }

    private Rect mChangeImageBackgroundRect = null;

    /**
     * 判断是否点击到了指定的控件上面
     */
    private boolean isInChangeImageZone(View view, int x, int y) {
        if (null == mChangeImageBackgroundRect) {
            mChangeImageBackgroundRect = new Rect();
        }
        view.getDrawingRect(mChangeImageBackgroundRect);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        mChangeImageBackgroundRect.left = location[0];
        mChangeImageBackgroundRect.top = location[1];
        mChangeImageBackgroundRect.right = mChangeImageBackgroundRect.right + location[0];
        mChangeImageBackgroundRect.bottom = mChangeImageBackgroundRect.bottom + location[1];
        return mChangeImageBackgroundRect.contains(x, y);
   }
 
 
 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值