manas-chaudhari / android-mvvm

MVVM on Android using RxJava and Data Binding

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to refresh RecyclerViewAdapter?

at-tuannguyen opened this issue · comments

Hi @manas-chaudhari,
I'm researching your project to implement MVVM to my project.
I saw you init data for RecyclerViewAdapter in ItemListViewModel at the same time activity create
So how I can automatically update RecyclerViewAdapter when I have a new data, example: I load data from server
Please help me. Thanks!

Hi @at-tuannguyen, the ItemListViewModel stores the list of items as Observable<List<ItemViewModel>> itemVms. Note that it is only constructing this observable from another Observable<List<Item>> named itemsSource. So, the constructor is only performing the conversion from Item to ItemViewModel.

I have initialized itemsSource statically in the same file for simplicity. You can have it passed to the ViewModel through the constructor and it can be generated from a database query OR network call. I have created an example for the same in DataLoadingViewModel.java/.

You can think of Observable as the latest value of a variable. Thus, all ViewModels will setup the conversions, mappings in the constructor itself (as if building an electronic circuit), and these mappings will get executed whenever data gets pushed into the root Observables (as if when power is connected to the circuit).

Thus, the RecyclerViewAdapter updates itself as soon as the data within Observable<List<ItemViewModel>> changes. This way, you don't have to worry about refreshing the RecyclerView. Just focus on constructing the right Observable.

Let me know if that clears your doubt.

Hi @manas-chaudhari,
Thank you for reply me soon. After a day research about ReactiveX, I realize I had been misunderstood it. My problem is solved. Your project is awesome, I learned a lot of things.
Thank you for sharing it!