Write your Generic Adapter (Android)

Atul Sakhala
2 min readAug 2, 2022

By kotlin uses generics and builds the most reusable design pattern in Android, Generic Adapter

So what is Adapter in Android?

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

How can we make this generic adapter?

The objective of this document is to leverage the creational design pattern and reuse the adapter components with less code.

In general, we use abstract classes/interfaces to make an adapter more plug n play. But less code requires more responsibility, and sometimes needs to create more dependencies, which means less flexibility, directly correlated to high maintenance, in general, high maintenance things have costs.

So I created GeneralAdapter using kotlin functionality i.e view binding, data binding, and the extension functions.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="object" <!--VariableId--->
type="Object" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main"
android:padding="10dp"
android:layout_margin="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardview_dark_background">
.............
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

So I first created an item.xml and we bind the

Then created a class with the name of GeneralAdapter<T> which extends the ListAdapter<T, ViewHolder>(DIFF_CALBBACK), it takes some optional parameters.

class GeneralAdapter<T>(
private val variableId:List<Int>,
private val layoutResource:List<Int>,
diffCallback: DiffUtil.ItemCallback<T>,
private val getItemTypeIndex: (T?) -> Int={0},
private val listOfClickableIds: List<Int> = mutableListOf()
) : ListAdapter<T, GeneralAdapter<T>.ViewHolder>(diffCallback)

Let me explain the purpose of these params,

  • variableId: That’s defined in the item.xml file. i.e it might be multiple so takes a list.
var variableId = mutableListOf(BR.object) // get the object in Fragment/Activity class.
  • layoutResource: That’s the item layout resource i.e we can define multiple layouts
//Resource layout i.e item.xml
private val layoutResource = mutableListOf(R.layout.item)
  • diffItemCallback: DiffUtil.ItemCallback is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second one. So we can write this in Object class.
data class Object(
val param1:String?,
val param2:String?)
{
companion object {

val DIFF_CALLBACK: DiffUtil.ItemCallback<CharacterUIModel> =
object : DiffUtil.ItemCallback<CharacterUIModel>() {
override fun areItemsTheSame(
@NonNull oldItem: Object,
@NonNull newItem: Object
): Boolean {
return oldItem.param1 == newItem.param1
&& oldItem.param2==newItem.param2

}

override fun areContentsTheSame(
@NonNull oldItem: Object,
@NonNull newItem: Object
): Boolean {
return oldItem == newItem
}
}
}
}
  • listOfClickableIds: Provides the list of ids to handle the clicks.
  • getItemTypeIndex: is the lambda function which return the itemType of different layouts.

So here’s a complete class.

Now you can plug n play to play around with any type of view either multiview or single view. GeneralAdapter can handle the situation best.

Press Clap if you like and have any doubt around then comment below

Thanks !! This the is a way.

--

--

Atul Sakhala

Android Developer with the fusion of Scientific Spirituality - FreeLancer, Founder of GameReel.gg and Tryhard.gg, Ex-Rivigo, Ex-BharatPe