An Android library for observable lists ultimately used with RecyclerView Adapters.
- Add the
jcenter()
repository to the projectbuild.gradle
file.
buildscript {
repositories { jcenter() }}
- Add the dependency to your module's
build.gradle
file.
dependencies {
implementation 'com.coenvk.android.observablist:observablist:1.0.0'}
The ObservableList
can be used to create a list which can be observed by a DataObserver
. The list extends the MutableList
class, so it can be used with the same functionality. For better performance, the item class can implement Diffable
and implement the areItemsTheSame
and areContentsTheSame
methods to compare two items.
Kotlin (click to expand)
val observableList = ObservableList<Any>("a", "b", 1, 2, 3)
val dataObserver = object : DataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
println("Items were removed.")
}
}
observableList.registerObserver(dataObserver)
observableList.remove("a")
// Output:
// Items were removed.
The ObservableList
is perfect for usage with a (RecyclerView) Adapter. Simply register an observer that notifies changes to the adapter and the view is automatically updated.
Kotlin (click to expand)
val dataObserver = object : DataObserver() {
override fun onChanged() {
adapter.notifyDataSetChanged()
}
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
adapter.notifyItemRangeRemoved(positionStart, itemCount)
}
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
adapter.notifyItemRangeInserted(positionStart, itemCount)
}
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
adapter.run {
for (i in 0 until itemCount) notifyItemMoved(fromPosition + i, toPosition + i)
}
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
adapter.notifyItemRangeChanged(positionStart, itemCount)
}
}
observableList.registerObserver(dataObserver)
The project can be build locally using the following commands:
$ git clone https://github.com/coenvk/observablist.git
$ cd observablist
$ ./gradlew clean build
Observablist is licensed under the Apache 2.0 License.