TheJadav

android BaseRecyclerView – genric recyclerview class with click listener

I have created generic BaseRecyclerView, but why we need it? We need to know in activity when item is clicked or may be we need to create replaceAll method which is very common for all recyclerview. In that case this will be very usefull

Generic Recyclerview

I have created generic recyclerview , but why we need it? We need to know in activity when item is clicked or may be we need to create replaceAll method which is very common for all recyclerview. In that case this will be very usefull.

 
import androidx.recyclerview.widget.RecyclerView

abstract class BaseRvAdapter<V: baseviewholder<T>, T> : RecyclerView.Adapter<V>(), BaseHolderListener {

    open val list: ArrayList<T> = ArrayList()

    override fun onBindViewHolder(holder: V, position: Int) {
        holder.setListener<V>(this)
        holder.setItem(list[position])
    }

    override fun getItemCount(): Int {
        return list.size
    }

    fun replaceAll(newList: ArrayList<T>){
        this.list.clear()
        this.list.addAll(newList)
        notifyDataSetChanged()
    }

    fun addAll(newList: List<T>) {
        addAll(list.size, newList)
    }

    fun addAll(position: Int, newList: List<T>) {
        this.list.addAll(position, newList)
        notifyDataSetChanged()
    }

    fun removeAll(newList: List<T>) {
        this.list.removeAll(newList)
        notifyDataSetChanged()
    }
}
import android.view.View
import androidx.recyclerview.widget.RecyclerView

abstract class BaseViewHolder<T>(view: View) : RecyclerView.ViewHolder(view) {
    private var listener: BaseHolderListener? = null

    fun <V> setListener(listener: BaseHolderListener) : Unit {
        this.listener = listener
    }

    abstract fun setItem(t: T)

    init {
        itemView.setOnClickListener {
            listener?.holderItemClicked(adapterPosition)
        }
    }
}
    
  
import android.os.Bundle

interface BaseHolderListener {
    fun holderItemClicked(position: Int, bundle: Bundle? = null)
}

Conclusion

It is very handy, we don’t need to worry about onBindViewHolder in Adapter, we don’t need to create list for adapter, just we need to check for our multiple views, everything else may be in this class, or we can also update this class for everything else, which is general in case.
Feel free to share your views about this.
you can find it on githubalso: BaseRecyclerView

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message