The role of adapters in the Mvp template?

How do you feel about adapters in the 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 a recyclierview as an element in this adapter (if you look at his project, he has a nested recyclierview on his home screen.) Since he has such an item, it performs a service call and other notifications, data loading (receiving from a service), etc. Operations in this adapter. (There is no connection with the corresponding fragment / activity) As you can see, this adapter has a lot to do. If you do this, how would you implement this in the mvp template? In particular, in this context, do you have a presenter object in the adapter or is there a view object for calling, notification, and loading?

+5
source share
1 answer

The adapter is a clean view. Match all interactions, like clicking on a button in a RecyclerView ViewHolder, back to Activity / Fragment, and then send it to the Activity / Fragment presentation.

RecyclerView is a bit more complicated because ViewHolders can be recycled while scrolling, etc. Returning to the “parent” Activity / Fragment and the corresponding Presenter, there are much simpler and fewer errors associated with updating the ViewHolder (i.e., using animations using DiffUtils). Take a ViewHolder in the same way as a data object, but do not add a Presenter for each ViewHolder to coordinate the ViewHolder. Indeed, just make sure that the ViewHolder receives a data object that contains all the information that the ViewHolder should display, but does not make this data object “controlled” by the ViewHolders Presenter. Otherwise, you get a mess because one ViewHolder receives an update from its Presenter, perhaps the ViewHolder was reworked in the average time, the screen orientation may have changed, or the “parent” Activity / Fragment Presenter updated the entire adapter dataset and etc. Do yourself a favor and use only one Presenter, which “coordinates / controls” the data that the RecyclerView should display using the “parent” Activity / Fragment Presenter.

+13
source

Source: https://habr.com/ru/post/1265707/


All Articles