OS 版本 4.4 (KitKat) 和版本 5 (Lollipop) onVisibilityChanged 侦听器未触发
OS version 4.4 (KitKat) and 5 (Lollipop) onVisibilityChanged Listener Not Firing
我在网上查找了这个问题的答案,但没有找到这个问题的根本原因。
我有一个带有视图的应用程序,它监听 onVisibilityChanged and onWindowVisibilityChanged。我注意到,在带有 KitKat (API 19) 或 Lollipop (API 22) 的 Android 设备上,当视图附加到主布局时,永远不会触发 onVisibilityChanged,只有 onWindowVisibilityChanged.
我猜这个问题源于从 KitKat 到 Lollipop 发生的本机 API 更改,但尚未找到任何文档或参考资料。当我在 Marshmallow (API 23) 上检查我的应用程序时,它没有发生。
我只想知道这是否是一个已知问题,或者是否有某种方法可以解决此问题。
谢谢。
看起来 Android 团队确实在 Android 23 - Marshmallow 中改变了这种行为。如果您查看 Android 23 中 View.java
的源代码,您可以看到他们在 dispatchAttachedToWindow
.
中添加了对 onVisibilityChanged
的调用
Here is the commit to AOSP where this change happened.
Here is the diff for that change.
鉴于您无法 post 您的代码,我无法提供比向您指出更改更好的解决方案。
如果您尝试跟踪附加到 window 的视图,我会建议 onAttachedToWindow
API。
注意Android23中调用onVisibilityChanged
的方法很有可能是dispatchAttachedToWindow
。此方法还调用 onAttachedToWindow
,因此理论上您可以监听此方法并手动检查可见性。
// Override inside your custom view
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Check for visibility here, but note that onVisibilityChanged initially gets called with View.GONE
// Unless the view is inside of a ViewGroup. See the source code for ViewRootImpl.java for more details.
}
}
否则,您将需要对 Android 22 及以下的逻辑进行特殊处理,以便您更多地收听 onWindowVisibilityChanged
而不是 onVisibilityChanged
。
我在网上查找了这个问题的答案,但没有找到这个问题的根本原因。
我有一个带有视图的应用程序,它监听 onVisibilityChanged and onWindowVisibilityChanged。我注意到,在带有 KitKat (API 19) 或 Lollipop (API 22) 的 Android 设备上,当视图附加到主布局时,永远不会触发 onVisibilityChanged,只有 onWindowVisibilityChanged.
我猜这个问题源于从 KitKat 到 Lollipop 发生的本机 API 更改,但尚未找到任何文档或参考资料。当我在 Marshmallow (API 23) 上检查我的应用程序时,它没有发生。
我只想知道这是否是一个已知问题,或者是否有某种方法可以解决此问题。
谢谢。
看起来 Android 团队确实在 Android 23 - Marshmallow 中改变了这种行为。如果您查看 Android 23 中 View.java
的源代码,您可以看到他们在 dispatchAttachedToWindow
.
onVisibilityChanged
的调用
Here is the commit to AOSP where this change happened.
Here is the diff for that change.
鉴于您无法 post 您的代码,我无法提供比向您指出更改更好的解决方案。
如果您尝试跟踪附加到 window 的视图,我会建议 onAttachedToWindow
API。
注意Android23中调用onVisibilityChanged
的方法很有可能是dispatchAttachedToWindow
。此方法还调用 onAttachedToWindow
,因此理论上您可以监听此方法并手动检查可见性。
// Override inside your custom view
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Check for visibility here, but note that onVisibilityChanged initially gets called with View.GONE
// Unless the view is inside of a ViewGroup. See the source code for ViewRootImpl.java for more details.
}
}
否则,您将需要对 Android 22 及以下的逻辑进行特殊处理,以便您更多地收听 onWindowVisibilityChanged
而不是 onVisibilityChanged
。