【问题标题】:Android: Data Binding Text not UpdatingAndroid:数据绑定文本未更新
【发布时间】:2016-10-28 23:56:43
【问题描述】:

我希望用户在EditText 中输入他们的姓名,并在TextView 中显示Hello {name}。当我将 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}"/>

【问题讨论】:

    标签: android data-binding android-databinding


    【解决方案1】:

    一个问题是您的 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 中监听 User 的更改会容易得多,而不必担心让 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>
    

    【讨论】:

    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多