Create dynamic lists with RecyclerView
RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they’re needed.
Key classes
Several different classes work together to build your dynamic list.
- RecyclerView is the ViewGroup that contains the views corresponding to your data. It’s a view itself, so you add RecyclerView into your layout the way you would add any other UI element.
- Each individual element in the list is defined by a view holder object. When the view holder is created, it doesn’t have any data associated with it. After the view holder is created, the RecyclerView binds it to its data. You define the view holder by extending RecyclerView.ViewHolder.
- The RecyclerView requests those views, and binds the views to their data, by calling methods in the adapter. You define the adapter by extending RecyclerView.Adapter.
- The layout manager arranges the individual elements in your list. You can use one of the layout managers provided by the RecyclerView library, or you can define your own. Layout managers are all based on the library’s LayoutManager abstract class.
Steps for implementing your RecyclerView
If you’re going to use RecyclerView, there are a few things you need to do. They’ll be discussed in detail in the following sections.
- First of all, decide what the list or grid is going to look like. Ordinarily you’ll be able to use one of the RecyclerView library’s standard layout managers.
- Design how each element in the list is going to look and behave. Based on this design, extend the ViewHolder class. Your version of ViewHolder provides all the functionality for your list items. Your view holder is a wrapper around a View, and that view is managed by RecyclerView.
- Define the Adapter that associates your data with the ViewHolder views.
Plan your layout
Implementing your adapter and view holder
Once you’ve determined your layout, you need to implement your Adapter and ViewHolder. These two classes work together to define how your data is displayed. The ViewHolder is a wrapper around a View that contains the layout for an individual item in the list. The Adapter creates ViewHolder objects as needed, and also sets the data for those views. The process of associating views to their data is called binding.
- When you define your adapter, you need to override three key methods:
- onCreateViewHolder()
- onBindViewHolder()
- getItemCount()