Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataBinding 与ViewModel 绑定 如何实现 #7

Open
jackerjin opened this issue Aug 17, 2020 · 2 comments
Open

dataBinding 与ViewModel 绑定 如何实现 #7

jackerjin opened this issue Aug 17, 2020 · 2 comments

Comments

@jackerjin
Copy link

报错 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
Copy link
Owner

没太理解 你要做什么

@jackerjin
Copy link
Author

没太理解 你要做什么
是这样 我想让 dataBinding 持有ViewModel

,mdataBinding.viewModel=mViewModel ,通过反射拿不到这个mViewModel ,通过反射也拿不到koin用的这个ViewModel 所以我只能改成这样写了, private val homeViewModel by viewModel() override fun initViewModel() { mViewDataBinding.run { viewModel = homeViewModel } },这样的话每个类里都有重复去写,网上查了也没有太好的方法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants