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

How does data persistence work in Android?

Data persistence in Android is the ability to save data to the device so that it can be accessed later, even if the app is closed or the device is restarted. There are several ways to achieve data persistence in Android

Fragments and Activities: Two Pillars of Android Development

Fragments and Activities are both important components of Android development. Fragments are a good choice for creating reusable and dynamic UIs, while Activities are a good choice for managing the lifecycle of an app’s UI and implementing core features.

Table of Contents

Send Us A Message