【发布时间】:2019-12-05 17:47:13
【问题描述】:
我正在构建一个 android 应用程序,但我不确定如何在 MVVM 架构中实现导航。 我采取的第一种方法是将点击事件按钮绑定到 ViewModel 中的一个函数,该函数在必要时执行一些逻辑(例如一些数据验证),然后触发一个 LiveData 事件(告诉视图导航到不同的屏幕)到观察 ViewModel 的视图。
<button android:id="@+id/btnId"
android:onClick="@{(v) -> myViewModel.onSaveClick()}"
.../>
class MyViewModel : ViewModel() {
val saveNavigation = MutableLiveData<Event<Customer>>()
val errorMessage = MutableLiveData<Event<String>>()
fun onSaveClick() {
if (validateCustomer(customer)) {
repository.save(customer)
saveNavigation.value = Event(customer)
}
else
errorMessage.value = Event("error")
}
}
class View : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//observe event protects from re-reading the value on screen rotation
myViewModel.saveNavigation.observeEvent(this) {
findNavController().navigate(CustomerViewDirections
.actionCustomerInfoToCustomerBalanceHistory(it))
}
myViewModel.errorMessage.observeEvent(this) { toast(it) }
}
}
第二种方法是视图注册到 onClickListener 并主动调用 ViewModel 逻辑函数(例如验证),然后视图才进行导航
class MyViewModel : ViewModel() {
fun save() : Status {
if (validateCustomer(customer)) {
repository.save(customer)
return Status.OK
}
else
return Status.Error //or some string message
}
}
class View : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
btnId.setOnClickListener {
if (myViewModel.save() == Status.OK)
findNavController().navigate(CustomerViewDirections
.actionCustomerInfoToCustomerBalanceHistory(myViewModel.customer))
else
toast("error")
}
}
}
这两种方式中哪一种更适合 MVVM 架构,或者可能还有其他选择?
【问题讨论】:
标签: android mvvm viewmodel android-livedata