我希望用户在EditText
中输入他们的名字,并在Hello {name}
内显示TextView
。当我将TextView
的文本设置为可观察属性(user.firstName
)时,文本会更新。即使我将文本设置为模型的toString()
方法,它仍然可以正常工作。但是,如果我改为使用viewModel.hello
,则文本不会更新。我错过了什么?
型号:
public class User extends BaseObservable {
public String firstName;
public String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Bindable
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
notifyPropertyChanged(BR.firstName);
}
@Bindable
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
notifyPropertyChanged(BR.lastName);
}
@Override
public String toString() {
return getFirstName() + " " + getLastName();
}
}
视图模型:
public class ViewModel {
public User user;
public ViewModel() {
user = new User("", "");
}
public String hello() {
return "Hello " + user.toString();
}
public TextWatcher userNameTextWatcher() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable.toString().length() == 0) {
user.setFirstName("");
user.setLastName("");
user.notifyChange();
return;
}
String[] name = editable.toString().split(" ");
if (name.length == 1) {
user.setFirstName(name[0]);
user.setLastName("");
user.notifyChange();
return;
}
String firstName = name[0] + " ";
for (int i = 0; i < name.length - 2; i++) {
firstName = firstName + " " + name[i] + " ";
}
user.setFirstName(firstName);
user.setLastName(name[name.length - 1]);
user.notifyChange();
}
};
}
}
布局:
<data>
<variable
name="viewModel"
type=".ViewModel"/>
</data>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="@{viewModel.hello}"/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="8dp"
app:addEditTextWatcher="@{viewModel.userNameTextWatcher}"/>
答案 0 :(得分:6)
一个问题是您的ViewModel不可观察,因此对hello()的更改不会反映在模型中。由于它是一个可观察的单个字段,因此您也可以使用ObservableField作为hello字符串,但我将使用Observable ViewModel演示它。
public class ViewModel extends BaseObservable {
public final User user;
public ViewModel() {
user = new User("", "");
user.addOnPropertyChangedCallback(new OnPropertyChangedCallback {
@Override
public void onPropertyChanged(Observable sender, int property) {
if (property == BR.firstName || property == BR.lastName) {
notifyPropertyChanged(ViewModel.this, BR.hello);
}
}
});
}
@Bindable
public String hello() {
return "Hello " + user.toString();
}
public TextWatcher userNameTextWatcher() {
return new TextWatcher() { ... }
};
}
}
但我认为直接在XML中监听对用户的更改会更容易,而不用担心使ViewModel可观察:
<data>
<variable
name="viewModel"
type=".ViewModel"/>
</data>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="@{@string/hello(viewModel.user.firstName, viewModel.user.lastName)}"/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="8dp"
app:addEditTextWatcher="@{viewModel.userNameTextWatcher}"/>
你有一个字符串资源:
<string name="hello">Hello %1$s %2$s</string>