Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fangzhzh/7bea917d94dd0f9e589d44af87d9c5f7 to your computer and use it in GitHub Desktop.
Save fangzhzh/7bea917d94dd0f9e589d44af87d9c5f7 to your computer and use it in GitHub Desktop.
Data Binding observables and Kotlin
// with just a simple extension function for the Data Binding ObservableField
inline fun <R> ObservableField<R>.observe(crossinline callback: (R) -> Unit) {
this.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(p0: Observable?, p1: Int) {
callback(get())
}
})
}
...
// observing the change of data is simple, clean and readable.
viewModel.user.observe {
//TODO use whatever with the observed data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment