Advance RecyclerView Adapter
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.
- Simple and easier implementation
- With MVVM Support
- With Data Binding Support
- Multi-View Support
- Long Click Support
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.thesarangal:broadrecycleradapter:VERSION_CODE'
}
Done! The first time you request a project JitPack checks out the code, builds it and serves the build artifacts (jar, aar).
Kotlin:
/* 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
Kotlin:
/* 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
Kotlin
/* 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
Note: If you are using Kotlin Coroutines, make sure that you adding items to adapter on Main-Thread.
Kotlin
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)
Kotlin
/* 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
Kotlin
val bottomOffsetDecoration = CustomSpaceDecorator.Builder()
.setSpacing(resources.getDimension(R.dimen._16dp).toInt())
.setOrientation(CustomSpaceDecorator.Orientation.VERTICAL)
.build()
binding.recyclerView.addItemDecoration(bottomOffsetDecoration)
android:onClick="@{(view) -> data.onItemClick(view)}"
adapter.add(/* OBJECT */)
adapter.addToTop(/* OBJECT */)
adapter.add(INDEX_INTEGER, /* OBJECT */)
adapter.addAll(/* LIST_OF_OBJECT */)
adapter.setItems(/* LIST OF OBJECT */)
adapter.remove(/* OBJECT */)
adapter.clear()
adapter.getItem(/* POSITION */)
adapter.getItems()
adapter.getMutableLiveDataList()
adapter.isNotEmpty()
adapter.itemCount
onItemClick(view: View)
onItemLongClick(view: View)
getViewHolder()
getAdapterItemList()
Contributions to this library are welcome. If you find a bug or have a feature request, please open an issue on the GitHub repository.
This library is released under the MIT License. You are free to use, modify, and distribute the library for commercial and non-commercial purposes, with attribution to the author.