关于这个问题,网上有很多例子,都是处理popwindow所依附的activity或fragment的touch事件。当然,通常情况都可以处理。
但是当在我的项目中加入了开源组件slidingmenu后,发现touch事件不能被正常触发了,这个时候该怎么办?
解决办法:定义PopupWindow水平和垂直都是MATCH_PARENT,然后设置监听PopupWindow的view点击事件,在该事件中关闭PopupWindow显示
缺点:popwindow所依附的activity或fragment没有任何点击或者触摸事件了。因为当前已经被PopupWindow 的view完全覆盖。
附上主要代码:
/** * 初始化功能菜单 */ public void initPopMenu() { menuPopWindow = new PopupWindow(menuWindow, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true); menuPopWindow.setOutsideTouchable(true); menuPopWindow.setFocusable(false); menuPopWindow.setAnimationStyle(R.style.AnimationPreview); menuPopWindow.update(); } private boolean togglePopMenu() { if (menuPopWindow != null && menuPopWindow.isShowing()) { menuPopWindow.dismiss(); return false; } else { menuPopWindow.showAtLocation(view, Gravity.RIGHT | Gravity.TOP, 0, 0); return true; } }
private void initPopMenuView(LayoutInflater inflater) { menuWindow = inflater.inflate(R.layout.pop_carmanage_menu, null); Button btn_search, btn_refresh, btn_add, btn_scan, btn_ok; btn_search = (Button) menuWindow.findViewById(R.id.btn_search); btn_refresh = (Button) menuWindow.findViewById(R.id.btn_refresh); btn_add = (Button) menuWindow.findViewById(R.id.btn_add); btn_scan = (Button) menuWindow.findViewById(R.id.btn_scan); btn_ok = (Button) menuWindow.findViewById(R.id.btn_ok); btn_search.setOnClickListener(this); btn_refresh.setOnClickListener(this); btn_add.setOnClickListener(this); btn_scan.setOnClickListener(this); btn_ok.setOnClickListener(this); initPopMenu(); menuWindow.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (!togglePopMenu()) { return true; } return false; } }); }