RecyclerView Adapter Android in Kotlin

In android recycler view is most commonly used and is important in almost all kinds of projects that’s why it is considered as the basic of android development.

Until unless we don’t understand the concept of working of android studio we can’t deal with it.
Once a recycler view concept is cleared anyone can do any customisation in data listing

Let’s move toward the topic first of all we have to discuss the basis of recycler view.

make the layout_custom.xml file to get ready your user interface

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_33sdp"
android:gravity="center"
android:layout_marginTop="@dimen/_5sdp"
android:orientation="horizontal"
android:weightSum="1">

<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:backgroundTint="@color/green"
android:text="Exercise"
android:paddingStart="@dimen/_7sdp"
android:paddingEnd="@dimen/_7sdp"
android:singleLine="true"
android:fontFamily="@font/helvetica"
android:textColor="@color/white"
android:textSize="@dimen/_14sdp"
android:textStyle="bold" />

</LinearLayout>
</layout>

Make a kotlin class for adapter and pass data that you want to use in the adapter as constructer parameter
We need a view holder class to bind the view with the adapter
In the next step extend this class with the recycler view adapter and typecast this adapter to the view holder class that you have created.

Implement the built in functions by ALT+ENTER.

class MyCustomAdapter(
val mContext: BaseActivity,
val mData: ArrayList<Any>,
val callBack: (position: Int, item: Any) -> Unit
) : RecyclerView.Adapter<MyViewHolder>() {

interface OnClickListener {
fun onItemClick(position: Int)
}



override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(context)
.inflate(R.layout.layout_custom, parent, false)
return MyViewHolder(view)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val item = mData[position]
//Do your work here
holder.binding.tvTitle.text = item.title
//and for callback
holder.binding.tvTitle.setOnClickListener{
callBack(position,item)
}
}
override fun getItemCount() = mData.count()
}
class MyViewHolder(ItemView: View):RecyclerView.ViewHolder(ItemView){
val binding:LayoutCustomBinding?=DataBindingUtil.bind(itemView)}

In case if you are not using data binding then simply use RecyclerView.ViewHolder everywhere instead of MyViewHolder

here onCreateViewHolder is returning your view to the adapter
getItemCount is simply returning your array count
onBindViewHolder to to set your data over the xml and for other actions like controlling the clicks and conditions , all your work will be done in this method

var mAdapter = MyCustomAdapter(this,mData){position,item->
}
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
recyclerView.adapter = mAdapter
//all set now when you do any change in the data just call notifydataset changed
mAdapter.notifyDataSetChanged

--

--

--

Being a mobile application developer I decided to write some useful content for those who are freshers in the fields and looking for helping material.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Android Code to save image in Internal Storage on button click

How to determine which Fragment is being Displayed in the Activity

How to create a Phone Call App in Kotlin Android?

11 Things You Should Know When Starting Android Development

Easy RecyclerView With DiffUtill and DataBiding(Kotlin)

How to add Cloud function in Android ?

Managing and Testing Translations in Android applications.

10 Flutter tips part — 4/10

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Munib Hamza

Munib Hamza

Being a mobile application developer I decided to write some useful content for those who are freshers in the fields and looking for helping material.

More from Medium

Android ViewModel

Make a simple RecyclerView in Android with Kotlin

View Binding in Android.

Kotlin Coroutines [Part-1]