A simple implementation of adapter for recycleview that implement DiffUtils and diffCallback by default.
The main idea of the library is to completely abandon the old implementation of updating the list via notifyDataChanged()
and simplify the process of creating a generic and multi-type list with multiple view types.
Also in a library implemented variation of selectable helper for adapter.
Gradle Groovy DSL:
implementation 'io.github.nuclominus:diffease:$diffeaseVersion'
Gradle Kotlin DSL:
implementation("io.github.nuclominus:diffease:$diffeaseVersion")
Using of this adapter is simple and almost does not differ from using a regular adapter. Using in three steps:
/**
* Implement adapter with specifying the data type and the supported data type.
* To support multiple cell types, we specify not an explicit viewholder type, but a generic one.
*/
class SimpleDiffAdapter() : DiffEaseAdapter<MultiMock, BaseViewHolder<MultiMock>>() {
...
}
In the adapter you can/should change the default implementation of the diff callback
// override default implementation
override fun <TInput : MultiMock> getDiffCallback(
oldItems: List<MultiMock>,
newItems: List<TInput>
): DiffResultCallback<MultiMock, TInput> = baseDiffCallback(oldItems, newItems)
Update adapter
vm.data.observe(viewLifecycleOwner) { data ->
adapter.update(data, true) // with detecting moves in list
}
You can find out about wider use in the Wiki
- Support several selectable adapters in concatadapter