
bug1文本框宽度设置wrap_content时显示不全解决约束布局规定尾部对齐同时设置宽度为0dpTextView ... android:layout_width0dp android:layout_heightwrap_content ... app:layout_constraintEnd_toEndOfparent /bug2RecyclerView使用分页查询数据首次进入界面列表项自动滚至首页底部修复后 -解决移除RecyclerView默认设置的动画binding.recyclerView.itemAnimator null需求1底部导航消息通知图标红点mainViewmodel定义livedata用于获取红点显示与否private val _showDot savedStateHandle.getLiveData(showDot, false) val showDot _showDot.distinctUntilChanged() fun updateDot(show: Boolean) { _showDot.value show }mainActivity订阅showDot定义底部徽章对应的ItemId颜色以及显隐参考文档https://github.com/material-components/material-components-android/blob/master/docs/components/BottomNavigation.mdvm.showDot.observe(this) { val bottomDot binding.bottomNavigationView.getOrCreateBadge(itemId) bottomDot.backgroundColor ContextCompat.getColor(this, R.color.dot) bottomDot.isVisible it }列表viewmodel定义livedata用于获取列表项数据// 用于内部数据设置 private val _data MutableStateFlowListT(emptyList()) // 暴露给外部仅可获取 val data _data.asLiveData(coroutineContext)列表fragment观察data根据列表项已读情况调用mainViewmodel的方法private fun setBottomDot() { vm.data.observe(this) { data - val aItem data.firstOrNull() as? ListItem.AItem val bList data.map { it as? ListItem.BItem } val show aItem?.showDot true || bList.any { it?.status ListDetails.Status.UNREAD } mainVm.updateDot(show) } }实现效果需求2返回时更新列表常驻项图标红点显示AItem点击后会跳转到A数据列表页面fragment2fragment2中检查列表项已读情况在返回时调用private fun checkRead() { val hasUnread adapter.items.any { it.status ListDetails.Status.UNREAD } val result bundleOf(HAS_UNREAD to hasUnread) parentFragmentManager.setFragmentResult(SHOULD_SHOW_DOT, result) }fragment1设置监听器进行获取和处理private fun setAItemDot() { parentFragmentManager.setFragmentResultListener( SHOULD_SHOW_DOT, this ) { requestKey, resultBundle - if (requestKey SHOULD_SHOW_DOT) { val hasUnread resultBundle.getBoolean(HAS_UNREAD) val item adapter.getItem(A_ITEM) as? ListItem.AItem ?: returnsetFragmentResultListener val updatedItem item.copy(showDot hasUnread) adapter[A_ITEM] updatedItem adapter.notifyItemChanged(A_ITEM) } } }实现效果全部已读 -通过Bundle传递数据对象需实现Parcelable接口Parcelize注解修饰依赖导入// libs.versions.toml [versions] kotlin your version ... [plugins] kotlin-android { id org.jetbrains.kotlin.android, version.ref kotlin } // build.gradle.kts(:Project) plugins { ... alias(libs.plugins.kotlin.android) apply false } // build.gradle.kts(:app) plugins { ... alias(libs.plugins.kotlin.android) id(kotlin-parcelize) }