View上具有可观察的实例字段的双向数据绑定?

问题描述 投票:0回答:1

[使用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,或者以某种方式可以简单地使用可观察的实例字段?在文档中,我总是看到...

android android-databinding android-binding-adapter
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.