一 假設(shè)
override fun setContentView(contentView: View?) {
super.setContentView(contentView)
contentView?.viewTreeObserver?.addOnGlobalFocusChangeListener { oldFocus, newFocus ->
doWork()
}
}
通過viewTreeObserver里進行全局焦點變化監(jiān)聽,但是會發(fā)現(xiàn),pop dismiss一次后,以后彈出來上述的焦點回調(diào)方法都不會調(diào)用了。
二 驗證
猜想是不是dismiss方法做了什么處理,于是查看源碼:
public void dismiss() {
//***
final View contentView = mContentView;
//***
dismissImmediate(decorView, contentHolder, contentView);
//**
}
/**
* Removes the popup from the window manager and tears down the supporting
* view hierarchy, if necessary.
*/
private void dismissImmediate(View decorView, ViewGroup contentHolder, View contentView) {
// If this method gets called and the decor view doesn't have a parent,
// then it was either never added or was already removed. That should
// never happen, but it's worth checking to avoid potential crashes.
if (decorView.getParent() != null) {
mWindowManager.removeViewImmediate(decorView);
}
if (contentHolder != null) {
contentHolder.removeView(contentView);
}
// This needs to stay until after all transitions have ended since we
// need the reference to cancel transitions in preparePopup().
mDecorView = null;
mBackgroundView = null;
mIsTransitioningToDismiss = false;
}
可以發(fā)現(xiàn),contentView的引用在dismiss時斷掉的。
所以顯然要想實現(xiàn)pop的焦點持續(xù)(不斷彈出/取消)監(jiān)聽,contentView?.viewTreeObserver?.addOnGlobalFocusChangeListener 就要具有即時性。那么show方法是不是會滿足每次調(diào)用的時候都會創(chuàng)建contentView新的執(zhí)行對象呢?
showAtLocation->preparePop->createBackgroundView(mContentView)->backgroundView.addView(contentView,*)->
// We may wrap that in another view, so we'll need to manually specify
// the surface insets.
WindowManager.LayoutParams對象.setSurfaceInsets(mBackgroundView, true /*manual*/, true /*preservePrevious*/);
可以看到,contentView是在show的時候第一次或者再次被添加到pop中去的。
但是即便contentView被斷掉了和pop的DecorView的聯(lián)系,其再次聯(lián)系上時,之前的監(jiān)聽怎么就無法生效了呢,此時應(yīng)該同樣能接收焦點事件的啊?
我們來做這樣一個監(jiān)聽:
override fun setContentView(contentView: View?) {
super.setContentView(contentView)
log("setContentView viewTreeObserver=${contentView?.viewTreeObserver}")
}
override fun showAtLocation(parent: View?, gravity: Int, x: Int, y: Int) {
log("showBefore viewTreeObserver=${contentView?.viewTreeObserver}")
super.showAtLocation(parent, gravity, x, y)
log("showAfter viewTreeObserver=${contentView?.viewTreeObserver}")
contentView?.viewTreeObserver?.addOnGlobalFocusChangeListener { oldFocus, newFocus ->
doWork()
}
}
日志輸出結(jié)果:
第一次創(chuàng)建并show
setContentView viewTreeObserver=android.view.ViewTreeObserver@b5a93e2
showBefore viewTreeObserver=android.view.ViewTreeObserver@b5a93e2
showAfter viewTreeObserver=android.view.ViewTreeObserver@b5a93e2
dismiss掉后第二次show
showBefore viewTreeObserver=android.view.ViewTreeObserver@3d4f5af
showAfter viewTreeObserver=android.view.ViewTreeObserver@3d4f5af
可以看到showBefore的時候id已經(jīng)發(fā)生了變化,所以說明在show之前已經(jīng)變化了。因此這就是監(jiān)聽不到的原因。
三 方案
因此,最終實現(xiàn)的方案就是:在show的時候再次addOnGlobalFocusChangeListener
override fun showAtLocation(parent: View?, gravity: Int, x: Int, y: Int) {
super.showAtLocation(parent, gravity, x, y)
contentView?.viewTreeObserver?.addOnGlobalFocusChangeListener { oldFocus, newFocus ->
doWork()
}
}
有沒有更優(yōu)雅的方式,畢竟還有showAsDropdown,難不成要一一去寫?
貌似還只能這么做。
另外pop中 EditText變化則沒有納入以上監(jiān)聽,所以還需要另外處理。