我正在尝试将 MVVM 应用于我的项目,并使用 LiveData 。但是,这样做时出现了问题。我有 : - LoginActivity ,其中具有 txtEmail 和 btnLogin ; - LoginViewModel 具有 电子邮件:MutableLiveData 。在活动中设置ViewModel后,请听btnLogin单击以设置 email.value =“ XXXX@YYY.com” 。我两次调试并中断了onChanged事件中的行跳转,但是下次单击时,不是那样的。
BaseActivity
abstract class BaseActivity<T : ViewDataBinding, V : BaseViewModel> : AppCompatActivity() {
private lateinit var mViewDataBinding: T
private lateinit var mViewModel: V
private var mProgressDialog: LoadingProgress? = null
override fun onCreate(savedInstanceState: Bundle?) {
// performDependencyInjection()
super.onCreate(savedInstanceState)
performDataBinding()
mProgressDialog = LoadingProgress(this)
}
private fun performDataBinding() {
mViewDataBinding = DataBindingUtil.setContentView(this, getLayoutId())
this.mViewModel = getViewModel()
mViewDataBinding.setVariable(getBindingVariable(), mViewModel)
mViewDataBinding.executePendingBindings()
mViewDataBinding.lifecycleOwner = this
}
/**
* Override for set binding variable
*
* @return variable id
*/
abstract fun getBindingVariable(): Int
/**
* @return layout resource id
*/
@LayoutRes
abstract fun getLayoutId(): Int
/**
* Override for set view model
*
* @return view model instance
*/
abstract fun getViewModel(): V
fun onFragmentAttached() {
}
fun onFragmentDetached(tag: String) {
}
protected fun getViewDataBinding(): T {
return mViewDataBinding
}
class LoginViewModel : BaseViewModel() {
var email= MutableLiveData<String>()
var password: MutableLiveData<String>? = null
fun onLoginClicked(){
email.value = "huypkhanh@gmail.com"
}
}
MainActivity
class MainActivity : BaseActivity<ActivityMainBinding, LoginViewModel>() {
private lateinit var mLoginViewModel: LoginViewModel
private lateinit var mActivityLoginBinding: ActivityMainBinding
override fun getBindingVariable(): Int {
return 1
}
override fun getLayoutId(): Int {
return R.layout.activity_main
}
override fun getViewModel(): LoginViewModel {
mLoginViewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)
return mLoginViewModel
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mActivityLoginBinding = getViewDataBinding()
mActivityLoginBinding.apply {
loginViewModel?.email?.observe(this@MainActivity, Observer { email ->
Log.i("aaa", email + hashCode())
})
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="loginViewModel"
type="com.nhathoang.loginmvvm.LoginViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/btn"
android:background="@color/colorPrimary"
android:textSize="16sp"
android:text="@={loginViewModel.email}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn"
app:layout_constraintTop_toTopOf="parent"
android:onClick="@{()-> loginViewModel.onLoginClicked()}"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
</layout>