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

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

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