Reputation: 73
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?
Upvotes: 7
Views: 3093
Reputation: 15919
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.
Upvotes: 15