/observablist

:eyeglasses: An Android library for observable lists ultimately used with RecyclerView Adapters.

Primary LanguageKotlinApache License 2.0Apache-2.0

Observablist 👓

An Android library for observable lists ultimately used with RecyclerView Adapters.

Bintray GitHub GitHub stars

Table of Contents

Getting Started

  1. Add the jcenter() repository to the project build.gradle file.
buildscript {  
 repositories { jcenter() }}  
  1. Add the dependency to your module's build.gradle file.

Latest version: Bintray

dependencies {  
 implementation 'com.coenvk.android.observablist:observablist:1.0.0'}  

Usage

Basic

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.

RecyclerView Adapter

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)

Build

The project can be build locally using the following commands:

$ git clone https://github.com/coenvk/observablist.git
$ cd observablist
$ ./gradlew clean build

License

Observablist is licensed under the Apache 2.0 License.