android - Android如何在不改变视图可见性的情况下调用绑定适配器?
问题描述
我正在使用 bindingAdapter 来设置基于state
.
但是 bindingAdapter 仅在视图重新膨胀或可见性更改时才起作用。有没有办法在不重新膨胀或改变可见性的情况下调用绑定?
@BindingAdapter("bgClickable")
fun bgClickable(layout: ConstraintLayout, state: String) {
when (state) {
DISCONNECTED -> {
val outValue = TypedValue()
layout.context.theme
.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true)
layout.setBackgroundResource(outValue.resourceId)
}
else -> {
layout.background = null
}
}
}
看法
.
.
.
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_location"
android:layout_width="wrap_content"
app:bgClickable="@{viewModel.getCurrentStatus()}"
android:layout_height="wrap_content"
android:onClick="@{()->viewModel.redirectToExplorer()}"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25">
<.../>
解决方案
绑定适配器的一个常见错误是不将 a 添加lifecycleOwner
到绑定对象,没有它,对底层实时数据的更改将不会通知绑定对象。所以在片段/活动类中添加以下内容
binding.lifecycleOwner = this
推荐阅读
- python - How to generate an animated curve using animation?
- java - keycloak 使用 securityCollections
- prometheus - 检查 ufw 状态并在未激活时发送 Prometheus 警报
- javascript - 想用更好的算子优化 Airtable 查询
- nuget-package-restore - NuGetAuthenticate 管道失败,无法获取本地颁发者证书
- docker - docker push to private registry 失败“服务器向 HTTPS 客户端提供 HTTP 响应”
- javascript - 画布上最后绘制的线是实心的?
- google-sheets - 如何避免必须将 0 放入 NULL 字段以在 Google 表格中获得正确的查询计算
- .net - Web 浏览器自动刷新崩溃
- asp.net-core - 以分块形式发送响应时,Asp.net 核心端点“挂起”