2015-01-17 25 views
1

让我们检查来自here一个简单的例子: 这里是一个TextView一个简单的布局文件绑定:RoboBinding如何视图模型知道变量返回

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

回答

1

你应该看看到整个源代码

MVVM example

有一个getter和于,在指定绑定的两种方式管理名称字段的值演示模型类二传手布局

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

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

相关问题