Android Kotlin RecyclerView MVP

Using MVP with RecyclerView is beneficial to clean code and testing. Without MVP, all your code will be inside the RecyclerView and a big mess, ah!

RecyclerView

Our RecyclerView should be all Android code. Their shouldn’t be any math, or calculations within the RecyclerView, only Android code. The RecyclerView code is often known as the interactor within MVP, but can be just called an adapter.

View / Activity

Our View should only code that directly affects the look and feel of the app. Their’s no math, or calculations involved in the Activity, Fragment, or View.

Within the onViewCreated() function we have a function being called into the presenter called presenter.showAllRentalProperties(). This function is used to get some data and feed it to the adapter. The next section will have code from the Presenter.

Presenter

We’re getting the information from the DB from the dbController. This is logic and deserves to be the Presenter. But the logic inside the dbController is purely Android code, the dbController code should be inside its own class.

We see that the adapter is being created from that list of rentalProperties as well, then it’s a parameter within a method to the object view. View is actually our view, the connection from the presenter to our Activity/Fragment.

View

Within our Fragment, this function is called:

This is purely Android code that interacts with what you see on the app screen. This belongs in the View because there isn’t any math or anything, it’s purely only there to display things on the screen.

Leave a Reply

Your email address will not be published. Required fields are marked *