【问题标题】:RoboBinding how does ViewModel know variable to returnRoboBinding ViewModel 如何知道要返回的变量
【发布时间】:2015-01-17 18:09:52
【问题描述】:

让我们看一个来自here 的简单示例: 这是一个带有 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 等,它怎么知道要绑定什么?

【问题讨论】:

    标签: android mvvm robobinding


    【解决方案1】:

    你应该看看整个源代码

    MVVM example

    表示模型类中有一个getter和一个setter,通过布局中指定的两种方式绑定来管理name字段的值

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

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

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      相关资源
      最近更新 更多