BroadRecyclerAdapter – Simplify RecyclerView with a Flexible Adapter
An open-source Kotlin library simplifying RecyclerView implementations with support for multiple view types

Introduction
Broad Recycler Adapter is a flexible and easy-to-use RecyclerView adapter for Android applications. It provides an easy way to handle different types of items within a single RecyclerView. It helps you to get all benefits of the RecyclerView without creating separate Adapter class for each RecyclerView.
Features
Simplified Implementation – Reduces boilerplate code, making RecyclerView setup more straightforward.
MVVM Support – Integrates seamlessly with the Model-View-ViewModel architecture.
Data Binding Support – Facilitates the use of Android’s Data Binding Library for binding UI components to data sources.
Multi-View Support – Manages multiple item view types within a single RecyclerView effortlessly.
Long Click Support – Provides built-in handling for long-click events on RecyclerView items.
How to use
Just Show Underline under specific word
/* Step 1. Create Class extend with BaseItemViewModel Class */
class TitleItemViewModel(val name: String) : BaseItemViewModel() {
/* Step 2. Return Layout Resource of the Item */
override val viewType: Int
get() = R.layout.inflate_title_view
}
/* Step 3. Binding Item Layout with Class (Which is created in Step. 1) */
<?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">
<data>
<variable
name="data"
type="in.sarangal.broadrecycleradaptersample.itemviewmodel.TitleItemViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:background="@android:color/holo_purple"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_16dp"
android:text="@{data.name}"
android:textStyle="bold"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
/* Step 4. Prepare List for Adapter */
val itemList = ArrayList<TitleItemViewModel>()
itemList.add(TitleItemViewModel("Title 1"))
itemList.add(TitleItemViewModel("Title 2"))
itemList.add(TitleItemViewModel("Title 3"))
itemList.add(TitleItemViewModel("Title 4"))
/* Step 5. Create Adapter Object and Set on RecyclerView */
val adapter = BroadRecyclerAdapter(itemList)
binding.recyclerView.adapter = adapter
Adding Simple Multi View List in Adapter
/* Step 1. Create First Class extend with BaseItemViewModel Class */
class TitleItemViewModel(val name: String) : BaseItemViewModel() {
/* Step 2. Return Layout Resource of the Item */
override val viewType: Int
get() = R.layout.inflate_title_view
}
/* Step 3. Create First Class extend with BaseItemViewModel Class */
class ContactItemViewModel(val name: String) : BaseItemViewModel() {
/* Step 4. Return Layout Resource of the Item */
override val viewType: Int
get() = R.layout.inflate_item_view
}
/* Step 5. Prepare List for Adapter */
val itemList = ArrayList<BaseItemViewModel>()
itemList.add(TitleItemViewModel("Title 1"))
itemList.add(ContactItemViewModel("Item Name 1"))
itemList.add(TitleItemViewModel("Title 3"))
itemList.add(ContactItemViewModel("Item Name 2"))
/* Step 6. Create Adapter Object and Set on RecyclerView */
val adapter = BroadRecyclerAdapter(itemList)
binding.recyclerView.adapter = adapter
Add Click Listener in Item
/* Step 1. Add Following Atributes in Item XML Layout */
android:clickable="true"
android:focusable="true"
android:onClick="@{(view) -> data.onItemClick(view)}"
/* Step 2. Add Click Interface in Adapter */
val adapter = BroadRecyclerAdapter(itemList,
itemClickListener = object : BaseItemClickListener {
override fun onItemClick(view: View, value: BaseItemViewModel) {
/* Step 4. Identify Multiple View Callback by Class Type */
if(value is ContactItemViewModel){
when(view.id){
/* Step 5. Identify Multiple Click by View Id */
R.id.delete -> {
/* Remove Item From Adapter */
viewModel.adapter?.remove(value)
}
else -> {
Toast.makeText(
this@MainActivity,
"Item Selected: ${value.checkBoxField.get()}",
Toast.LENGTH_LONG
).show()
}
}
}
}
})
binding.recyclerView.adapter = adapter
Add Items Dynamically
Note: If you are using Kotlin Coroutines, make sure that you adding items to adapter on Main-Thread.
val adapter = BroadRecyclerAdapter(itemList)
...
/* Adding Single Item */
adapter.add(TitleItemViewModel("Title 1"))
/* Adding List of Item */
val itemList = ArrayList<BaseItemViewModel>()
itemList.add(TitleItemViewModel("Title 1"))
itemList.add(ContactItemViewModel("Item Name 1"))
itemList.add(TitleItemViewModel("Title 3"))
itemList.add(ContactItemViewModel("Item Name 2"))
adapter.addAll(itemList)
Add Long Click Listener in Item
/* Step 1. Add Following Atributes in Item XML Layout */
android:clickable="true"
android:focusable="true"
app:onLongClick="@{() -> data.onItemLongClick(ID_OF_VIEW)}"
e.g.
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
app:onLongClick="@{() -> data.onItemLongClick(info)}"/>
/* Step 2. Add Click Interface in Adapter */
val adapter = BroadRecyclerAdapter(
itemClickListener = object : ItemLongClickListener {
/* Step 3. Long Click Call Back */
override fun onItemLongClick(view: View, value: BaseItemViewModel) {
/* Step 4. Identify Multiple View Callback by Class Type */
if (value is ContactItemViewModel) {
/* Step 5. Identify Multiple Click by View Id */
when (view.id) {
R.id.info -> {
Toast.makeText(
this@MainActivity,
"Item Long Pressed",
Toast.LENGTH_LONG
).show()
}
}
}
}
/* Step 6. Single Click Call Back */
override fun onItemClick(view: View, value: BaseItemViewModel) {
/* Step 7. Identify Multiple View Callback by Class Type */
if (value is ContactItemViewModel) {
/* Step 8. Identify Multiple Click by View Id */
when (view.id) {
R.id.delete -> {
/* Remove Item From Adapter */
viewModel.adapter?.remove(value)
}
R.id.info -> {
Toast.makeText(
this@MainActivity,
"Please long press on the button",
Toast.LENGTH_LONG
).show()
}
else -> {
Toast.makeText(
this@MainActivity,
"Item Selected: ${value.checkBoxField.get()}",
Toast.LENGTH_LONG
).show()
}
}
}
}
}
)
binding.recyclerView.adapter = adapter
Use CustomSpaceDecorator Class to Spacing(Margin) around the item layout
val bottomOffsetDecoration = CustomSpaceDecorator.Builder()
.setSpacing(resources.getDimension(R.dimen._16dp).toInt())
.setOrientation(CustomSpaceDecorator.Orientation.VERTICAL)
.build()
binding.recyclerView.addItemDecoration(bottomOffsetDecoration)
Crafted In
Let’s Improve Together
Your reactions guide development. Please share your thoughts on new features or recommendations. Unitedly we can improve to help all.