The Recyclerview is the replacement of the Listview with a simpler API, better performance and a couple of new nice extra features. If you don’t know what a ListView is, don’t worry, you won’t need to in order to understand the recyclerview, simply think of it as a UI component used to represent related items in a list with scrolling capabilities.
Components of a Recyclerview
The Recyclerview is mainly used to list related elements together, however, the way you do it can be dramatically customized to make your users have a unique experience in your app. Things such as determine how the elements are arranged together, enable custom views per element, respond to touch gestures, among other options, are available for you.
These options are not directly accessible from the Recyclerview object itself, instead, a group of related objects is used for each specific configuration, allowing for cleaner code and reusability.
Along the course of series we will be working with the most relevant components of the Recyclerview but for now, I’ll leave a little description as a tease to what we will working with next, here they are:
ViewHolder
The view holder object is a static representation of an element inside the Recyclerview, accessible from kotlin or java code. The main responsibility of the ViewHolder is to display each element with bound data from the adapter object.
Adapter
The Recyclerview doesn’t work with the data directly, it is only responsible for displaying it on the screen. The data management is done via an adapter object which in turn uses ViewHolder objects as containers for each element’s data. The adapter is tasked with three things:
- Serve as an Intermediary between the model data and the Recyclerview object.
- Bind the provided data to each ViewHolder object.
- Determine which XML layout should be used to load the ViewHolders.
LayoutManager
The Recyclerview is in charge of the display of the elements of the list but the way it does that is delegated to a LayoutManager object. Android provides a few layout managers built-in such as the LinearLayoutManager
and GridLayoutManager
but you can also provide your own if you want a more tailored experience for your Recyclerview.
ItemTouchHelper
Once we have our adapter setup and our layout manager selected, we can enable our Recyclerview to respond to touch gestures such as dragging elements or swiping to the side. For these purposes, we use the ItemTouchHelper object.
Next Step
Now that we have a rough understanding of what a Recyclerview is, what the components are and what they can do, we are ready to start a small coding adventure.
On the following post, we will be working with the Recyclerview’s ViewHolder & Adapter components to create a simple list of elements to put all this theory into action.
Top comments (0)