You need to enable JavaScript to run this app. WindowInsetsControllerCompat.OnControllableInsetsChangedListener在Insets显示时不触发问题
优惠活动
大模型
产品
解决方案
定价
更多

WindowInsetsControllerCompat.OnControllableInsetsChangedListener在Insets显示时不触发问题

问题原因

  • 你混淆了回调的触发场景:WindowInsetsControllerCompat.OnControllableInsetsChangedListener 仅在应用可控制的inset类型发生变更时触发,不是系统栏的可见性变化时触发。用户点击屏幕弹出导航栏时,仅改变了系统栏的可见状态,系统没有收回你对系统栏的控制权,所以不会触发该回调。
  • 部分定制ROM对系统栏的弹出逻辑做了修改,会拦截官方原生的回调事件,也会导致该场景下回调不触发。

解决方案

  1. 替换为监听系统栏可见性变化的回调,不要依赖控制权变更回调。在页面根View上设置OnApplyWindowInsetsListener监听即可覆盖所有系统栏显隐场景:
ViewCompat.setOnApplyWindowInsetsListener(rootView) { _, insets ->
    val isSysBarVisible = insets.isVisible(WindowInsetsCompat.Type.navigationBars()) 
        || insets.isVisible(WindowInsetsCompat.Type.statusBars())
    if (isSysBarVisible) {
        // 先移除已有延时任务避免重复触发
        hideAfterAWhile.removeCallbacksAndMessages(null)
        hideAfterAWhile.postDelayed({
            insetsController?.hide(
                WindowInsetsCompat.Type.navigationBars() or WindowInsetsCompat.Type.statusBars()
            )
        }, 3000)
    }
    insets
}
  1. 配置正确的系统栏行为,确保交互逻辑符合预期:
// 设置用户滑动/点击弹出的系统栏为临时显示模式
insetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
  1. 页面销毁时及时移除监听器和延时任务,避免内存泄漏:
override fun onDestroyView() {
    super.onDestroyView()
    insetsController?.removeOnControllableInsetsChangedListener(listener)
    hideAfterAWhile.removeCallbacksAndMessages(null)
}

内容的提问来源于stack exchange,提问作者Arek Kubiński

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 05:06:03