What is the purpose of the Recyclerview.Adapter class when using Recyclerview?

Asked

Viewed 1,240 times

6

To use the Recylerview Android we need to create a class CustomAdapter extending from the abstract class RecyclerView.Adapter then implement three methods that are:

  • onCreateViewHolder
  • onBindViewHolder
  • getItemCount

The method getItemCount i know it returns the amount of items in Recyclerview, but the Adapter class itself and the methods onCreateViewHolder and onBindViewHolder leaves me confused and with doubts regarding its purposes and workings, these doubts will be addressed below.

Doubts

  1. What is the purpose of the class CustomAdapter when will we use the Recyclerview?
  2. What is the purpose of the method onCreateViewHolder?
  3. What is the purpose of the method onBindViewHolder?

2 answers

6


What is the purpose of the Customadapter class when using Recyclerview?

Initially Adapter, or adapter, let’s say so, is responsible for making a display for each item in a data set. That is, one it represents the connection between the View and some data source, which usually come in two types: represented by data based on array’s or lists; or represented by cursor-based data.

With that in mind, the name Customadapter already expressed its meaning, making it explicit that it deals with a type of Adapter, but customized, that is, a custom adapter. There is a Simpleadapter, is a simple adapter used to customize list or grid items. This adapter acts as a bridge between a AdapterView and the data intended for that View. The CustomAdapter is a little beyond, because it gives more authority over the views, so you can make any kind of modification to the item.

What is the purpose of the onCreateViewHolder method?

The method onCreateViewHolder() serves to inflate the layout item. Basically it is called when it is necessary to create a new item.

What is the purpose of the onBindViewHolder method?

The method onBindViewHolder() has the purpose of defining the display attributes based on the data. Basically it is invoked when an item needs to be displayed to the user.

3

What is the purpose of the Customadapter class when using Recyclerview?

A Recyclerview must be able to visually present a variety of data types and in different ways.
For this to be possible to implement in an easy way, Recyclerview, uses an object of type Recyclerview.Adapter.
Its function is to take the data, coming from any data source, and "transform" them into views to be used by Recyclerview.

What is the purpose of the onCreateViewHolder method?
What is the purpose of the onBindViewHolder method?

These two methods are the "heart" of the Adapter, it is its implementation that serves the purpose of the Adapter.

The method onCreateViewHolder() is called by Recyclerview when it needs a new view.
The method onBindViewHolder() is called by Recyclerview when it needs new data to be assigned to a view.

Both use an object Recyclerview.Viewholder where "are stored" the views.
In the method onCreateViewHolder() Viewholder is created and then passed to the method onBindViewHolder() when it is necessary to assign data to views.
The use of Viewholder, in conjunction with the two methods, allows unused views to be reused when necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.