API 29下ViewCompat.setOnApplyWindowInsetsListener键盘弹出时未触发
解决API 29中ViewCompat.setOnApplyWindowInsetsListener键盘弹出不触发问题
以下是针对该问题的具体解决步骤:
确保根布局不自动消费系统Insets
打开页面根布局(ConstraintLayout)的XML文件,添加或修改属性:android:fitsSystemWindows="false"避免根布局自动处理Insets,导致监听无法接收到回调。
主动触发Insets请求
在调用setUiWindowInsets()方法后,添加代码强制请求Insets派发:private fun setUiWindowInsets() { ViewCompat.setOnApplyWindowInsetsListener(binding.root) { _, insets -> // 原有逻辑保留 prevPosition = (binding.rvComments.layoutManager as LinearLayoutManager).findLastVisibleItemPosition() binding.container.updateLayoutParams<ViewGroup.MarginLayoutParams> { currentBottomMargin = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom updateMargins(bottom = currentBottomMargin) binding.rvComments.scrollToPosition(prevPosition) } insets } // 主动请求Insets派发 ViewCompat.requestApplyInsets(binding.root) }配置Activity的软键盘模式
在AndroidManifest.xml中对应Activity的标签里,添加或修改windowSoftInputMode属性为adjustResize,确保键盘弹出时系统会调整布局并派发Insets:<activity android:name=".YourTargetActivity" android:windowSoftInputMode="adjustResize"> </activity>排查其他拦截Insets的View
确认页面内的可滚动控件(如RecyclerView、ScrollView)未设置fitsSystemWindows="true",避免这些控件提前消费Insets事件,导致根布局的监听无法触发。
内容的提问来源于stack exchange,提问作者cr7semen
相关产品推荐
相关产品推荐

