天天看點

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 [email protected]

showBefore [email protected]

showAfter [email protected]

dismiss掉後第二次show

showBefore [email protected]

showAfter [email protected]

可以看到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變化則沒有納入以上監聽,是以還需要另外處理。

Android 如何監聽popupwindow的焦點變化