【发布时间】:2020-02-21 10:18:12
【问题描述】:
我正在尝试根据在下面的 BaseObservable 类中定义的布尔值动态设置和更改背景可绘制对象,但我收到错误消息,指出它无法对“isFollowing”方法进行双向数据绑定。
错误是这样的:
表达式 'viewModel.isFollowing()' 不能倒置,所以它 不能用于双向绑定详细信息:双向绑定不能 解析布尔属性“isFollowing”的设置器
private var isFollowing: Boolean = false
@Bindable
fun isFollowing(): Boolean {
return isFollowing
}
@BindingAdapter("android:background")
fun setIsFollowing(frameLayout: FrameLayout, isFollowing: Boolean) {
if (this.isFollowing != isFollowing) {
this.isFollowing = isFollowing
if(this.isFollowing){
frameLayout.background = frameLayout.context.getDrawable(R.drawable.following)
}else{
frameLayout.background = frameLayout.context.getDrawable(R.drawable.follow)
}
}
}
这是我的布局文件
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@={viewModel.isFollowing}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
我按照建议尝试了这个,但没有任何乐趣:
companion object {
private var isFollow: Boolean = false
@JvmStatic
val isFollowing: Boolean
@Bindable get() = isFollow
}
【问题讨论】:
标签: android data-binding