Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The role of adapters in Mvp pattern?

Tags:

android

mvp

How do you treat adapters in MVP pattern? For example, in this project https://github.com/msahakyan/nested-recycler-view there is a MovieAdapter, https://github.com/msahakyan/nested-recycler-view/blob/master/app/src/main/java/com/android/msahakyan/nestedrecycler/adapter/MovieAdapter.java this guy has an recyclierview as item in this adapter , (if you look at his project, he has nested recyclierview in his home screen.) Because he has such an item, he doing service call and other notify, loading data(fetching from service) etc. operation in this adapter.(No communicaation to related fragment/activity) As you can see, this adapter has lots of to do. If you would do that, how would you implement this in mvp pattern? Specifically, in this context, would you have presenter object in adapter, or have view object to do that calling,notifying and loading?

like image 767
Bayram İnanç Avatar asked Mar 20 '17 15:03

Bayram İnanç


People also ask

What is MVP design pattern?

Interface is defined in presenter class, to which it pass the required data. Activity/fragment or any other view component implement this interface and renders the data in a way they want. In the MVP design pattern, the presenter manipulates the model and also updates the view.

What is MVP pattern in Android?

MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.

What does the presenter do in MVP?

Presenter: The last part is the presenter, which handles UI updates based on changes to the data model, and also processes users inputs. The presenter will contain much of the business code and replaces the controller from MVC.

What is presenter in Android?

A Presenter is used to generate View s and bind Objects to them on demand. It is closely related to the concept of an RecyclerView. Adapter, but is not position-based. The leanback framework implements the adapter concept using ObjectAdapter which refers to a Presenter (or PresenterSelector ) instance.


1 Answers

Adapter is pure View layer. Map all interaction like a click on a button in a RecyclerView ViewHolder back to the Activity / Fragment and then let forward that to the Activity's / Fragment's presenter.

RecyclerView are a little bit trickier because the ViewHolders can be recycled during scrolling etc. By going the road back to the "parent" Activity / Fragment and the corresponding Presenter it is much easier and less error prone to update a ViewHolder (i.e. with animations by using DiffUtils ). Take a ViewHolder just as a way to display a data object but don't add a Presenter for each ViewHolder to coordinate the ViewHolder. Really, just ensure that the ViewHolder gets a data object containing all information the ViewHolder needs to display but don't make this data object be "controlled" by a ViewHolders Presenter. Otherwise, you end up with a mess because one ViewHolder gets update by his Presenter , maybe the ViewHolder has been recycled in the mean time, maybe a screen orientation change has occurred or maybe the "parent" Activity / Fragment Presenter has updated the adapter's whole dataset etc. Do yourself a favor and use only one Presenter "coordinating / controlling" the data ther RecyclerView should display by using "parents" Activity / Fragment Presenter.

like image 168
sockeqwe Avatar answered Sep 22 '22 03:09

sockeqwe