Recycler Adapter

This recyclerview is a simplified version of the recyclerview that is often used to create lists, which only requires a few lines of code that are already prepared and can be customized.

For Example:

The first step is to create a view of the recyclerview using an xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingHorizontal="16dp"
    android:paddingTop="16dp"
    android:background="?selectableItemBackground"
    xmlns:tools="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="This is a text" />

    <com.google.android.material.divider.MaterialDivider
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title"
        android:layout_marginTop="12dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Then, you can create an class of recyclerview and extend to BaseAsyncRecyclerAdapter. For example:

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.wahidabd.library.presentation.adapter.BaseAsyncRecyclerAdapter
import com.wahidabd.library.presentation.adapter.viewholder.BaseAsyncItemViewHolder
import com.wahidabd.onelibrary.databinding.ItemRecyclerBinding

class TestBaseAsyncRecyclerAdapter(
    private val context: Context
) : BaseAsyncRecyclerAdapter<String, TestBaseAsyncRecyclerAdapter.ViewHolder>(){

    override fun getViewBinding(parent: ViewGroup, viewType: Int): ViewBinding =
        ItemRecyclerBinding.inflate(LayoutInflater.from(context), parent, false)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ViewHolder(getViewBinding(parent, viewType))

    inner class ViewHolder(binding: ViewBinding) : BaseAsyncItemViewHolder<String>(binding = binding){
        override fun bind(data: String) {
            with(binding as ItemRecyclerBinding){
                tvTitle.text = data
            }
        }
    }

}

Recyclerview and event listener

If you need events to handle clicks and other things, you can use lambda expressions as parameters. For example:

class TestBaseAsyncRecyclerAdapter(
    private val context: Context,
    private val onItemClicked: (String) -> Unit
) : BaseAsyncRecyclerAdapter<String, TestBaseAsyncRecyclerAdapter.ViewHolder>(){

    override fun getViewBinding(parent: ViewGroup, viewType: Int): ViewBinding =
        ItemRecyclerBinding.inflate(LayoutInflater.from(context), parent, false)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ViewHolder(getViewBinding(parent, viewType))

    inner class ViewHolder(binding: ViewBinding) : BaseAsyncItemViewHolder<String>(binding = binding){
        override fun bind(data: String) {
            with(binding as ItemRecyclerBinding){
                tvTitle.text = data

                tvTitle.onClick {
                    onItemClicked.invoke(data)
                }
            }
        }
    }

}

After the adapter is successfully created, you can call the adapter on the Activity/Fragment to enter data according to your needs, to use it you can use kotlin lazy. For example:

// Activity/Fragment
private val asyncAdapter: TestBaseAsyncRecyclerAdapter by lazy {
    TestBaseAsyncRecyclerAdapter(
        context = this,
        onItemClicked = {}
    )
}

// and insert data 
override fun initObservers() {
    asyncAdapter.setData = Constant.dataList()
}

Last updated