RecyclerView Adapter for RxJava, separates list's model from Android framework dependencies
Supports RxJava2 and automatic adapter notify dispatching (via DiffUtils comparison)
First, import the library via gradle dependency
compile 'sk.teamsoft:observablecollection:{latest_version}'
Check Sample app for both simple and advanced use-cases.
- Create source (here,
Data
is the model class)
val source = SimpleAdapterSource<Data>(emptyList(), R.layout.view_data)
- Create Adapter and set to RecyclerView
recyclerView.adapter = ObservableAdapter(source)
- Prepare View
<sk.teamsoft.observableadapterdemo.simple.DataView ...>
<TextView android:id="@+id/data" .../>
</sk.teamsoft.observableadapterdemo.simple.DataView>
class DataView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr), BindableView<Data> {
private val label: TextView by lazy { findViewById(R.id.data) }
private val detail: TextView by lazy { findViewById(R.id.data_detail) }
override val observableEvent: Observable<Any>?
get() = RxView.clicks(this)
override fun bindTo(item: Data) {
label.text = item.label
detail.text = item.detail
}
}
- Set data
source.data = listOf(data1, data2, data3)
Take a look at Advanced demo
Team-SOFT s.r.o.
dusan@teamsoft.sk