我正在尝试在我的自定义控件中使用 native 2 向 android 数据绑定(bind)
所以我在 xml 中有类似的东西
<layout>
<data>
<variable name="item" type="Boolean"/>
</data>
...
<my.control app:vm="@={item}"/>
...
</layout>
请注意,这是关于 @={}
- native 2 向绑定(bind)的问题。
还有类似的代码:
class MyControl extends RelativeLayout{
...
@BindingAdapter("app:vm")
public static void setVm(View v, VM vm){...}
}
我的问题 - 我应该如何为我的 viewModel 定义 getter?我找不到任何关于它的指导。我尝试了不同的方法 - 编写自定义 getter、静态 getter 但错误仍然相同。
最佳答案
取自here ,在“滚动你自己的”下:
您需要更多的额外代码才能使双向数据绑定(bind)与自定义类一起使用。最重要的是,您需要定义一个 @InverseBindingMethod
:
@InverseBindingMethods({
@InverseBindingMethod(type = MyControl.class, attribute = "vm"),
})
In this case, the name of the getter matches the name of the attribute “getVm” for “app:vm.” (Changed to your example)
请访问链接的博客 - 它有关于该主题的更多信息,包括属性更改事件监听器的绑定(bind)。
关于java - 找不到值为 boolean 值类型的属性 'app:vm' 的 GETTER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38055194/