Closed vejei closed 4 years ago
有类似的问题,也想请教下~
@Froyo91 官网文档:https://developer.android.com/topic/libraries/architecture/viewmodel#implement
Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.
维基百科:https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
The main difference between the view model and the Presenter in the MVP pattern, is that the presenter has a reference to a view whereas the view model does not. Instead, a view directly binds to properties on the view model to send and receive updates. To function efficiently, this requires a binding technology or generating boilerplate code to do the binding.
如果Toast
也算作View
或者View
的一部分,这个就不能说是MVVM
不好意思,这边写的确实不是很严格,toast和跳转简单写了,严格上应该按接口方式放在view层去做的。在实际项目中,取决模块复杂程度程度,太严格遵守可能反而增加成长,这个要具体衡量,框架上也可以自由调整,我这个只是参考哈。
感谢回答 @KelinHong @zeleven
ViewModel
不是不能引用View
也不关心View
的吗?那为什么NewsViewModel
和NewsItemViewModel
会有这样的代码:这里竟然弹出
Toast
?难道Toast不算View
或者View
的一部分?startActivity
,也就是跳转到新的页面不是View
的工作?为什么会由ViewModel
来做?这样还算是
MVVM
吗