Why RecyclerView Adapter is called an Adapter

about why recycler view adapter is called recycler view adapter

YE MON KYAW
Arpalar Tech
4 min readApr 4, 2023

--

Do you know why we are very used to with Adapter (ArrayAdapter, RecyclerView Adapter ) in Android Development, have you ever think why we call as Adapter?

In this article let me explain we call as Adapter, RecyclerView Adapter is called an adapter because it follows the Adapter Design Pattern, which is a design pattern used to bridge incompatible interfaces.

In Android, Adapters are used to populate views such as ListViews, GridViews, and RecyclerViews with data. An Adapter acts as a bridge between the data source and the view, and it converts the data into a format that the view can use.

Let me explain a bit about what is Adapters Design Patterns, as mention as above the adapter design pattern is a structural design pattern that allows incompatible interfaces to work together.

It enables objects with incompatible interfaces to collaborate by creating a middleman that translates the interface of one object into an interface that the other object can understand.If you still don’t get the point, no worry i will explain with example:

In the context of a Car, an Adapter Design Pattern can be used to make the Car object work with other objects that have incompatible interfaces.

class Car {
fun start() {
println("Starting the car...")
}

fun stop() {
println("Stopping the car...")
}

fun fillFuel() {
println("Filling up the gas tank...")
}

fun drive() {
start()
println("Driving the car...")
stop()
}
}

The start(), stop(), and fillFuel() methods work with the Car object's internal combustion engine. However, we also have an ElectricCar object that has a different interface with the following methods:

interface ElectricCar {
fun charge()
fun drive()
}

The charge() method is used to charge the electric car's batteries, while the drive() method is used to start driving the car. These methods are incompatible with the Car object's methods.

To make the Car object work with the ElectricCar object, we can create an adapter class that implements the ElectricCar interface and translates the charge() method to the fillFuel() method of the Car object.

class ElectricCarAdapter(private val car: Car) : ElectricCar {
override fun charge() {
car.fillFuel()
}

override fun drive() {
car.drive()
}
}

In this adapter class, we take a Car object as a parameter in the constructor and implement the charge() and drive() methods of the ElectricCar interface. The charge() method is implemented by calling the car.fillFuel() method, which fills up the gas tank of the Car object. The drive() method is simply translated to a call to the car.drive() method.

Now, we can create an instance of the ElectricCarAdapter class and pass it a Car object to make the two objects work together:

val car = Car()
val electricCar = ElectricCarAdapter(car)

electricCar.charge() // The adapter translates the method call to the Car object's fillFuel() method.
electricCar.drive() // The adapter translates the method call to the Car object's drive() method.

In this example, the ElectricCar object’s charge() method call is translated to a call to the Car object's fillFuel() method by the adapter class. Similarly, the drive() method call is translated to a call to the Car object's drive() method. This allows us to use the ElectricCar object's interface to work with the Car object's internal combustion engine.

After reading this hope you will be understand how Adapter Design useful and how it works. So let continue about how the Adapter Design Pattern is applied in the RecyclerView:

  1. Data source: The RecyclerView.Adapter takes data from a data source, which can be a list, array, database, or any other data source.
  2. View creation: The RecyclerView.Adapter creates views for each item in the data source. This is done in the onCreateViewHolder() method, which inflates the layout for the view and creates a ViewHolder object to hold the view.
  3. View binding: The RecyclerView.Adapter binds data from the data source to each view. This is done in the onBindViewHolder() method, which retrieves the data for the current position and binds it to the view.
  4. View recycling: The RecyclerView.Adapter recycles views that have scrolled off the screen to save memory and improve performance. This is done in the onBindViewHolder() method as well, by reusing the ViewHolder object for a new view at a different position.

The RecyclerView.Adapter class is responsible for bridging the gap between the data source and the RecyclerView by converting the data into a format that can be displayed in a list or grid format.

It does this by creating and managing views for each item in the data source and binding data to each view as it is displayed on the screen. This makes it easier to display large amounts of data in a scrollable list or grid format in Android applications.

This implementation of the Adapter Design Pattern enables the RecyclerView or ListView to display data from a variety of data sources, including arrays, lists, and databases. By using an Adapter, the View can remain decoupled from the data source, which promotes maintainability and flexibility in the application.

--

--

YE MON KYAW
Arpalar Tech

Software Engineer who write code in Kotlin / Android