RoboBinding ViewModel 如何知道变量到 return

RoboBinding how does ViewModel know variable to return

让我们看一下 here 中的一个简单示例: 这是一个带有文本视图绑定的简单布局文件:

<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 等,它怎么知道要绑定什么?

你应该看看整个源代码

MVVM example

表示模型class中有一个getter和一个setter,它们使用布局中指定的两种绑定方式管理名称字段的值

        <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        bind:text="${name}"/>

如果您有更多变量,如果您想使用双向绑定,则必须为每个变量提供 getter 和 setter。