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

来源:互联网 发布:简单的js特效 编辑:程序博客网 时间:2024/04/26 15:01

如果出现第一次点击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