android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.
Apache License 2.0
5.06k stars 3.01k forks source link

What is the best way to use liveData and viewModel in RecyclerView's ViewHolders. #190

Open AlexMa1755 opened 3 years ago

AlexMa1755 commented 3 years ago

I am wondering if there is a best way to use LiveData and ViewModel to update RecyclerView's ViewHolders. Because in a complex ViewHolder, it is much better to let the ViewModel controls the ViewHolder to update it's UI. Also , when the ViewHolder wants to change the source in ViewModel. It can get ViewModel and call the related change method in ViewModel. the ViewHolder only need to observe the liveData in ViewModel. But I do not know how to write these code in the best way.

Yan9080 commented 3 years ago

Have you checked RecyclerView's codelabs ? It may help you to find a proper way to work with,