dataBinding 与ViewModel 绑定 如何实现
Opened this issue · 2 comments
jackerjin commented
报错 Cannot create an instance of class com.wanandroid.module_home.HomeViewModel,好像
val type = (this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
mViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application)
.create(type[0] as Class)
拿不到class类,我想用ViewModel去实现双向绑定,在BaseActivity 或者BaseFragment 不太好拿到这个class
winlee28 commented
没太理解 你要做什么
jackerjin commented
,mdataBinding.viewModel=mViewModel ,通过反射拿不到这个mViewModel ,通过反射也拿不到koin用的这个ViewModel 所以我只能改成这样写了, private val homeViewModel by viewModel() override fun initViewModel() { mViewDataBinding.run { viewModel = homeViewModel } },这样的话每个类里都有重复去写,网上查了也没有太好的方法没太理解 你要做什么
是这样 我想让 dataBinding 持有ViewModel