Recycler View and Adapter with Model pattern for Android

Recycler View is used as the List View. The difference between the two is that the RecyclerView is using less hardware resources for showing the items in the list. For example, if you have list with 500 items, its not wise for the app to load every time the whole list. The Recycler View is loading items as the user swipes, up or down so, the list is not populated all of the time, but the user have feeling that it is, and the hardware is not overloaded.

As for the Adapter, is used for populating programmatically the items in the list, what it shows and what it does.

The populating the item in the list we will use a Model class for storing the info that we will need to pass to the Adapter and finally the RecyclerView to show us in the activity, or the fragment or the view that we create. In our case we will use Activity, or more precisely, MainActivity.

In the end we must have hardcoded List<> of Models so we must pass to the Adapter. This list can come from everywhere, like, in our case, hardcoded, or some other activities or some web services.

Here is the example, step by step explanations:

First we need to set the RecyclerView in our activity_layout.xml in Linear Layout. You can use Relative Layout or something else if you like.

After this, we need to define the Model so it can hold our data. The model will have two Text Fields for name and address and one Button, just for example. The string fields will be declaring public so we can access them from anywhere in our app, and final so that string that can only be assigned once, in our constructor. So, when we create an object from Person class we will be obliged to give it two strings as parameters, name and address. The Model (Person) will look like this:

We need to make the layout for the item to be shown in the Recycler View. The layout will have two Text Fields and one Button in layout/item_person.xml. It is in Relative Layout overall, two text views wrapped in Linear Layout, and a Button on the far right side. (For the layouts, in detail, in some of the next posts):

After all of this is done, now we can jump to creating the Adapter for our app. First of all, we need to create PersonAdapter class that’s extends from RecyclerView.Adapter. As type in the <> brackets we will add PersonAdapter.ViewHolder, and ViewHolder will be our inner class, which will be explained later. After this we will need to override 3 methods from RecyclerView.Adapter:
onCreateViewHolder() for inflating the layout that we create earlier item_person.xml
onBindViewHolder() for binding functionalities that we will define in our inner ViewHolder class
getItemCount() for getting the items in the List that we are going to give to the RecyclerView.
Then we create ViewHolder inner class that extends RecyclerView.ViewHolder that must create constructor matching super. In our PersonAdapter class we will add our own method addAll(List personModelList) so we can add the whole list to the adapter.
In our PersonAdapter we must declare a List of type Person for our model list, and a Context so the adapter will know in witch activity, fragment or view we want to initialize the list. This two fields are initialized in the PersonAdapter constructor, for context we use this, and for the list we initialize the list in it.
In onCreateViewHolder(), which is overridden method and receive parameters like ViewGroup parent and int viewType, which will be used to inflate the view for the item.
In onBindViewHolder() we bind the view for the item, with its parameters, views, like TextView, Buttons or whatever we like, and implement the functionality of the item in the RecyclerView, and that is accomplished by a method called from ViewHolder class bind().
In getItemCount() we return the length of the list, so the RecyclerView and the Adapter will know how many items the list have.
In our addAll() method, which takes List as parameter, so we can manipulate with the List, or to clear, to add items, and notify the adapter if items are added or deleted from the list.
In our inner class ViewHolder, we declare all of the views in the item, the Person model and one int which will be the position of the item. We initialize two Text Views, name and address, and initialize the Button. In the constructor we declare ButterKnife.bind() so the views are imported to the parent activity. Bind method is for functionality implementation of the item, and there we will set the name and address fields when adapter received the List.

Finally, we implement functionality of the RecyclerView and Adapter in our activity. First declare the recyclerView from activity_layout.xml and PersonAdapter. We create our own methods for setting up user interface setupUi() and method for hardcoded list of items getHardcodedList().
In setupUi() we create the layout manager to handle the separation of item in the RecyclerView, give the adapter all items in the list, set the recyclerView and use the Divider Item Decoration to divide the items in the recyclerView horizontally by line.

NOTE: Im using ButterKnife library for binding the views, like buttons, text, etc.


Spread the love

Leave a Reply

Your email address will not be published.