Cannot find the GETTER for attribute 'app:vm' with value type Boolean

流过昼夜 提交于 2019-12-23 09:58:31

问题


I'm trying to use native 2-way android data binding in my custom control

so I have something like that in xml

<layout>
<data>
<variable name="item" type="Boolean"/>
</data>
...
<my.control app:vm="@={item}"/>
...
</layout>

Please note, it's question about @={} - native 2 way binding.


and something like that in code:

class MyControl extends RelativeLayout{
...
@BindingAdapter("app:vm")
public static void setVm(View v, VM vm){...}
}

My questions - how should I define getter for my viewModel? I don't find any guidance about it. I tryed different approaches - write custom getter, static getters but error still the same.


回答1:


Taken from here, under "Rolling Your Own":

You'll need a little more extra code to get the two-way databinding working with custom classes. Most importantly, you'll need to define a @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)

Please visit the linked blog- it has more information on that topic, including the binding of a attribute changed event listener.



来源:https://stackoverflow.com/questions/38055194/cannot-find-the-getter-for-attribute-appvm-with-value-type-boolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!