[英]RoboBinding how does ViewModel know variable to return
讓我們從這里檢查一個簡單的示例:這是一個帶有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等,怎么辦?它將如何知道要綁定的內容?
您應該看一下整個源代碼
表示模型類中有一個getter和setter,它們使用布局中指定的兩種綁定方式來管理name字段的值
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
bind:text="${name}"/>
如果您有更多變量,則要使用雙向綁定,必須為每個變量提供一個getter和setter。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.