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

共同学习、探讨

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ganshenml

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值