這是我最近遇到的問題,由于之前對PopupWindow使用不熟悉,理解不透徹導(dǎo)致的,所以現(xiàn)在對這些方法進(jìn)行了盡量的深入解析,另外總結(jié)就是不能只管去拷貝復(fù)制別人的代碼,然后發(fā)現(xiàn)不符合自己的要求了,就一個(gè)一個(gè)去改著試試,這樣盡管最后可能達(dá)到了要求,但卻不知其所以然,后續(xù)還極有可能突然就蹦出來個(gè)bug。
- 是否正確理解
setFocusable(boolean focusable)和setOutsideTouchable(boolean touchable)?
要徹底弄清這個(gè)問題,就要分別查看android5.0以下和5.0以上(含5.0)的源碼,因?yàn)閍ndroid系統(tǒng)創(chuàng)建popup的時(shí)候在5.0以上和以上會有不同的處理,5.0以下的系統(tǒng)在創(chuàng)建popup的時(shí)候會根據(jù)你是否通過調(diào)用popup的setBackgroundDrawable(Drawable background)方法來判斷是否把你的popup放到一個(gè)PopupViewContainer里面,5.0以下的源碼如下(對無關(guān)代碼做了省略):
private void preparePopup(WindowManager.LayoutParams p) {
......
if (mBackground != null) {
PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, height
);
popupViewContainer.setBackgroundDrawable(mBackground);
popupViewContainer.addView(mContentView, listParams); //重點(diǎn),只有mBackground != null時(shí)才會執(zhí)行
mPopupView = popupViewContainer;
} else {
mPopupView = mContentView;
}
}
而5.0以上(含5.0)的代碼是這樣的:
private void preparePopup(WindowManager.LayoutParams p) {
.......
if (mBackground != null) {
mBackgroundView = createBackgroundView(mContentView);
mBackgroundView.setBackground(mBackground);
} else {
mBackgroundView = mContentView;
}
mDecorView = createDecorView(mBackgroundView); //重點(diǎn),和mBackground無關(guān),都會執(zhí)行
}
5.0以上系統(tǒng)的PopupDecorView就是5.0以下的PopupViewContainer,它們是同一個(gè)東西,這個(gè)containerView繼承自FrameLlayout,它對onTouchEvent方法進(jìn)行了重寫:
private class PopupDecorView extends FrameLayout {
......
@Override
public boolean onTouchEvent(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
if ((event.getAction() == MotionEvent.ACTION_DOWN)
&& ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
dismiss();
return true;
} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
dismiss();
return true;
} else {
return super.onTouchEvent(event);
}
}
看到這,恍然大悟,在5.0以下的系統(tǒng),當(dāng)你沒有setBackgroundDrawable時(shí),此時(shí)的popup是完全沒有處理你的屏幕點(diǎn)擊觸摸事件的能力的,包括你的物理返回按鍵也一樣不會響應(yīng),這時(shí)候不管你去如何設(shè)置setOutsideTouchable都是沒有意義的,根本不會進(jìn)入到onTouchEvent方法里面,也就走不到if (event.getAction() == MotionEvent.ACTION_OUTSIDE)這個(gè)邏輯判斷了。
這個(gè)MotionEvent.ACTION_OUTSIDE很熟悉,就是通過這個(gè)action來判斷是否響應(yīng)外側(cè)點(diǎn)擊事件的,那么它是怎么觸發(fā)的呢?
當(dāng)把 MotionEvent.ACTION_OUTSIDE與setOutsideTouchable(boolean touchable)放到一起,就會很明了了。
分析setOutsideTouchable方法:
先看看setOutsideTouchable(boolean touchable)的源碼:
public void setOutsideTouchable(boolean touchable) {
mOutsideTouchable = touchable;
}
然后再看看mOutsideTouchable哪里會用到
private int computeFlags(int curFlags) {
curFlags &= ~(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
…………
if (mOutsideTouchable) {
curFlags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
}
…………
return curFlags;
}
這段代碼主要是用各種變量來設(shè)置window所使用的flag;
既然說到了FLAG_WATCH_OUTSIDE_TOUCH,那我們來看看FLAG_WATCH_OUTSIDE_TOUCH所代表的意義:

這段話的意思是說,如果窗體設(shè)置了FLAG_WATCH_OUTSIDE_TOUCH這個(gè)flag,那么 用戶點(diǎn)擊窗體以外的位置時(shí),將會在窗體的MotionEvent中收到MotionEvetn.ACTION_OUTSIDE這個(gè)事件。
所以由于在PopupViewContainer中添加了對MotionEvent.ACTION_OUTSIDE事件的捕捉,在當(dāng)用戶點(diǎn)擊PopupViewContainer以外的區(qū)域時(shí),PopupWindow這個(gè)窗體就會收到ACTION_OUTSIDE事件,然后調(diào)用dismiss方法來關(guān)閉掉自己。
讀到這,基本對setOutsideTouchable方法的來龍去脈了解的差不多了。
分析setFocusable方法:
再看setFocusable方法,一般資料對此的解釋都是:是否讓Popupwindow獲得焦點(diǎn),這確實(shí)揭示了本質(zhì),但卻很抽象,不能從方法調(diào)用的級別來解釋該方法的作用。其實(shí),這個(gè)方法的觸發(fā)以及接收處理和setOutsideTouchable方法原理是一樣的:
public void setFocusable(boolean focusable) {
mFocusable = focusable;
}
接下來設(shè)置它的flag
if (!mFocusable) {
curFlags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
if (mInputMethodMode == INPUT_METHOD_NEEDED) {
curFlags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
}
}
這個(gè)flag比較牛逼,它能接收屏幕的 ACTION_UP,ACTION_DOWN等Touch事件,所以當(dāng)設(shè)置了setFoucus為true時(shí),因?yàn)镻opupViewContainer對MotionEvent.ACTION_DOWN事件進(jìn)行了捕捉,然后同樣會調(diào)用自己的dissmiss方法來關(guān)閉自己。
現(xiàn)在也就很好明白為什么在設(shè)置了setFoucus(true)后再去設(shè)置 setOutsideTouchable(false)為什么沒有作用了,因?yàn)镸otionEvent.ACTION_DOWN事件在MotionEvent.ACTION_OUTSIDE事件之前處理,消耗了MotionEvent事件。這樣也就能解釋官方對setOutsideTouchable方法的說明了

"控制是否通知popup窗體外的點(diǎn)擊事件,這個(gè)方法只有在touchable為true而focusable為false的時(shí)候才有意義", 說的很嚴(yán)謹(jǐn),是有沒有意義而不是有沒有作用,