[使用Android双向数据绑定时,我必须在BindingAdapter
上使用静态View
还是以某种方式可以简单地使用可观察的实例字段?在文档中,我总是只在ViewModel
上看到可观察的字段,而不是在View
上看到。我尝试使用[]在View
上实现可观察字段
var myValue: String = "" @Bindable get(): String { return field } set(value: String) { field=value setText(value) notifyPropertyChanged(BR.myValue) // my View implements the Observable interface }
但是当我编译它时(用
./gradlew assembleDebug --stacktrace
获取详细信息,它失败并显示
ERROR: Cannot find a getter for <com.example.test.MyAutoCompleteTextView app:myValue> that accepts parameter type 'java.lang.String' If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.
因此,是否不可能像
View
一样在双向数据绑定的ViewModel
端使用可观察字段?我想使用可观察字段而不是静态BindingAdapter
的原因是,我的View
具有比我在BindingAdapter
中可以处理的逻辑/状态更复杂的逻辑/状态(从静态BindingAdapter
中我可以调用到myViewInstance.myValue
,但对我来说有点不对劲)
使用Android双向数据绑定时,我是否必须在View上使用静态BindingAdapters,或者以某种方式可以简单地使用可观察的实例字段?在文档中,我总是看到...