让我们从这里检查一个简单的例子:这是一个带有 textview 绑定的简单布局文件:
<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 等,它怎么知道要绑定什么?