With the stack of MVVM + LiveData + Repository sample being adopted so usually, it has confirmed to be one particular of the most perfect architectures in the Android environment. Hundreds and countless numbers of apps out there are dependent on this architecture and have been doing genuinely wonderful so far.
The implementation of this architecture and circulation has currently been described in hundreds of article content so I won’t select to reinvent the wheel. If you want to discover far more about it, then I’d suggest one particular of the best posts that I’ve examine so considerably is Android Architecture starring Kotlin Coroutines, Jetpack (MVVM, Room, Paging), Retrofit and Dagger 2. To aid the intent of the remedy in this article, I’m just symbolizing a extremely primary illustration of this flow right here.
With this above illustration, you can see that the moment the ViewModel gets the info from the Repository, then we update its LiveData with the reaction gained. Because that LiveData is consistently getting observed by the Fragment/Action, at the time the information is observed, it passes that to the RecyclerView’s adapter. Then the adapter utilizes that dataset to map every single product on to the perspective in onBindViewHolder().
Though functioning on 1 of the Blogs applications, it was heading good until I arrived throughout a problem wherever the knowledge that was handed to the adapter from Fragment was not sufficient. So the need was to display the list of authors alongside with the amount of their posts. You may well be questioning, alright so what is the trouble right here gentleman, there will have to be some home like posts_depend that you can simply map on to the TextView 🤷🏻♂️. But it wasn’t as you and I anticipated, there wasn’t any home like this existing in the listing endpoint. I would regard it to be a negative API design and style but since it was a 3rd occasion API so we could not do something a lot about it. So in order to exhibit the posts rely versus just about every creator, I needed to connect with the get_posts_depend endpoint with authod_id currently being passed as the question parameter, period.
// Shut up and acquire my cash
Whilst you can get the full source code from the Github repository, for the sake of simplicity, I’ll just add below the code snippets necessary to clarify the alternative.
PostsListFragment.kt
After the put up reaction is obtained from the listing endpoint, we pass the information to mapDataOnRecyclerView(). There we initialize the adapter and move the posts facts to its constructor. Despite the fact that in observe, we deal with the initialization of the adapter making use of Dependency Injection, for the sake of simplicity, I’ve held it uncomplicated and just initialized the adapter with new.
PostsAdapter.kt
Along with the dataset and click on listener(higher-get perform), we are passing the PostsViewModel and LifecycleOwner objects as nicely.
So why pass the LifecycleOwner? To perform with the ViewModel, we need to have to attach it with some LifecycleOwner object like Exercise, Fragment, and so forth. As the RecyclerView.Adapter is not a LifecycleOwner, we need to have to pass the LifecycleOwner instance of regardless of what the UI part is initializing the adapter which in our situation, is the PostsFragment. With this technique, our adapter is created automatically lifecycle-knowledgeable as properly mainly because as the fragment stops/destroys, the viewLifecycleOwner will cease listening.
PostsViewModel.kt
As we scroll by way of the RecyclerView, when a new product exhibits up, we phone this strategy from the adapter that returns the posts_depend of each author.
I hope this post presents a very good knowledge of how to execute networking in Adapter. A person crucial thing to point out more than right here is, you can undertake the exact same technique in any other class that is not a LifecycleOwner. It could be a custom ArrayAdapter, ViewPagerAdapter, customized sights, etcetera.
Be sure to share your solutions in the opinions.
The full source code of this project can be observed on the Github repo.
If you come across this tutorial practical your 👏 will be a great deal appreciated and inspiring for the upcoming articles!
Be sure to really feel free of charge to access out to me on LinkedIn.