让我们研究一下这里中的一个简单示例:下面是一个带有文本视图绑定的简单布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://robobinding.org/android">
<TextView
bind:text="{hello}" />
...
<Button
android:text="Say Hello"
bind:onClick="sayHello"/>
下面是这个布局的视图模型:
@org.robobinding.annotation.PresentationModel
public class PresentationModel implements HasPresentationModelChangeSupport {
private String name;//how does framework now what to set name to ?
public String getHello() {
return name + ": hello Android MVVM(Presentation Model)!";
}
...
public void sayHello() {
firePropertyChange("hello");
}
}我的问题是,viewModel如何知道名称是什么?它没有被设置在任何地方?如果我有许多变量,如name2、name3等,那么它怎么知道绑定什么呢?
发布于 2015-03-17 00:05:59
您应该查看整个源代码。
表示模型类中有一个getter和setter,它们使用布局中指定的两种绑定方式管理name字段的值。
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
bind:text="${name}"/>如果您有更多的变量,您必须为每个变量提供一个getter和setter,如果您想使用双向绑定。
https://stackoverflow.com/questions/28002611
复制相似问题