Android 如何監聽popupwindow的焦點變化

一 假設

override fun setContentView(contentView: View?) {
super.setContentView(contentView)
contentView?.viewTreeObserver?.addOnGlobalFocusChangeListener { oldFocus, newFocus ->
        doWork()
}
}

通過viewTreeObserver里進行全局焦點變化監聽,但是會發現,pop dismiss一次后,以后彈出來上述的焦點回調方法都不會調用了。

二 驗證

猜想是不是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;
}

可以發現,contentView的引用在dismiss時斷掉的。
所以顯然要想實現pop的焦點持續(不斷彈出/取消)監聽,contentView?.viewTreeObserver?.addOnGlobalFocusChangeListener 就要具有即時性。那么show方法是不是會滿足每次調用的時候都會創建contentView新的執行對象呢?

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的聯系,其再次聯系上時,之前的監聽怎么就無法生效了呢,此時應該同樣能接收焦點事件的啊?

我們來做這樣一個監聽:

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()
}
}

日志輸出結果:

第一次創建并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已經發生了變化,所以說明在show之前已經變化了。因此這就是監聽不到的原因。

三 方案

因此,最終實現的方案就是:在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()
}
}

有沒有更優雅的方式,畢竟還有showAsDropdown,難不成要一一去寫?
貌似還只能這么做。

另外pop中 EditText變化則沒有納入以上監聽,所以還需要另外處理。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容