MVVM design pattern implementation in kotlin for a recyclerview

Answer #1 100 %

The core about MVVM is seperation of concerns. ViewModel should not hold any reference to the View(Activity/Fragment). LikeWise your Data/Repository layer should not hold ViewModel reference.

So to achieve data flow you can use either Reactive Observables(Rx)/ LiveData from android architecture components to pass back the data.

1) Create MutableLiveData in your Viewmodel.

2) Set the MutableLiveData with api response model.

3) Observe the MutableLiveData in your Fragment for the response data.

4) Use the data to set your adapter inside your fragment.

Please check ViewModel - Developer document to understand better.

You’ll also like:

© 2023 -