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变化则没有纳入以上监听,所以还需要另外处理。

共同学习、探讨

猜你喜欢

转载自blog.csdn.net/ganshenml/article/details/117266683