How To Use The RecyclerView Adapter

The RecyclerView.Adapter class is used to bind a dataset to a RecyclerView to be displayed to a user. As I explained in another post, RecyclerView Vs ListView, the RecyclerView.Adapter forces the use of the ViewHolder pattern, which may be hard to understand when switching to a RecyclerView from a ListView. In this short post I am going to reference my MovieAdapter class from my Swipe-To-Dismiss example and break it down to explain the required implementations and how to use the RecyclerView adapter.

The Adapter Class

Let’s first take a look at the class header for the adapter:

The class extends from RecyclerView.Adapter, which should be the obvious part here. However, you’ll notice that class requires a generic type that must extend from RecyclerView.ViewHolder.

The RecyclerView.ViewHolder

The ViewHolder is a class that represents layout information for an item in the RecyclerView. The use of the ViewHolder is that so a new layout isn’t inflated for each and every item, but the same layout is just reused and the attributes of an element (such as the text in a TextView) are repopulated for each item. This is a huge performance helper and enables for much smoother scrolling. Let’s take a look at our ViewHolder:

The class, as I mentioned earlier, extends from RecyclerView.ViewHolder. In this case, we only have one element for each item that must be populated, the movie’s name. Inside this class, you put all UI elements that will be manipulated at the class level, and you instantiate them inside of your constructor (which takes a View parameter).

I have added the bindMovie() method here as an option to make my program more readable. This method takes a Movie object, and populates all of the necessary UI elements for that movie. If you don’t want to implement this method, you can handle this in onBindViewHolder explained later.

The Constructor

In a basic adapter, you will need two class level variables. A Context, likely the Activity the adapter belongs to, which is used for inflating layouts, and a List<Object> or some other representation of your data source. You can use a simple constructor to instantiate these fields:

getItemCount

Of the three methods required for implementation, getItemCount() is perhaps the easiest. All you have to do is return the size of your dataset, so the Adapter knows how many items to populate:

If you used an array, you can just change this line to return mMovies.length;

onCreateViewHolder

When the Adapter creates its first item, onCreateViewHolder is called. This is what allows the Adapter to reuse a reference to a view instead of re-inflating it as I mentioned earlier. Typically this implementation will just inflate a view and return a ViewHolder object:

 

You’ll notice that there is a second parameter for viewType. This parameter is used if you are displaying different types of items inside your list, meaning you’ll have to create a second ViewHolder, and use this parameter to determine which of them to return. Using multiple ViewHolders may be explained in a later post.

onBindViewHolder

The onBindViewHolder method is where the magic happens. This is where you get an item from your data set and bind it to the list item in your adapter, like this:

The position parameter represents the position of the item being bound at the time. You use this parameter to get the proper item from your data set. The holder parameter is the ViewHolder your item will be bound to. So, in this method you can either call holder.movieNameTextView.setText(mMovie.getPosition); and all other declarations, or you can make use of the bindMovie method like I did.

Once you’ve added the ViewHolder and implemented these three required methods, you now know how to use the RecyclerView adapter! This is a very powerful tool that will make your displays much more flexible down the line.

Share this post...
Share on Reddit0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Facebook0

1 Comment

Leave a Reply