Android双向绑定:观察对象字段更改

时间:2019-07-28 09:59:10

标签: android android-databinding

我目前正在尝试对模型对象使用Android双向绑定,并且难以观察对象字段中的更改。我的模型对象在ViewModel中定义为MutableLiveData。

在下面的示例中,xml布局中的edittext_email小部件绑定到了ViewModel中的userModel.email属性。 在MainActivity中可以看到userModel。当电子邮件EditText更改时,不会触发观察者的onChanged()方法。我假设观察到对象时似乎未触发对象字段更改。

有一个好的解决方法吗?

谢谢!

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
                name="userViewModel"
                type="com.example.test1.MainViewModel" />
    </data>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/text_input_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:errorText="@{userViewModel.emailErrorText}"
                    app:errorEnabled="true">
                <EditText
                        android:id="@+id/edittext_email"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Email Address"
                        android:inputType="textEmailAddress"
                        android:text="@={userViewModel.userModel.email}" />
            </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>
</layout>

class MainViewModel {

    var userModel : MutableLiveData<UserModel>? = null
    var emailErrorText : MutableLiveData<String>? = null

    fun setUser(user: UserModel){
        this.userModel = MutableLiveData(user)
    }

    fun setEmailErrorText(errorText: String){
        this.emailErrorText = MutableLiveData(errorText)
    }

    fun getEmail():String{
        return userModel?.value!!.email
    }
}


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val user = UserModel("frank@mail.com")
        val viewModel = MainViewModel()
        viewModel.setUser(user)

        val binding: com.example.test1.databinding.ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        binding.userViewModel = viewModel
        binding.lifecycleOwner = this

        viewModel.userModel?.observe(this, object : Observer<UserModel> {
            override
            fun onChanged(@Nullable user: UserModel) {
                Log.d("MainActivity","changed")

                if (user.email == null || user.email.length == 0){
                    viewModel.setEmailErrorText("Email cannot be empty")
                    return
                }

                val isValidEmail = Patterns.EMAIL_ADDRESS.matcher(viewModel.getEmail()).matches()
                if (!isValidEmail){
                    viewModel.setEmailErrorText("Email should be valid")
                    return
                }
            }
        })
    }

0 个答案:

没有答案