Issue
It's possible to make BindingAdapter
with LiveData
Observer call?
I need LifecycleOwner
to call LiveData Observer.
The BindingAdapter:
@BindingAdapter("app:progressBarObserver")
fun setProgressBarObserver(progressBar: ProgressBar, observer: MutableLiveData<Boolean>) {
observer.observe({lifecycle_here}, Observer {
if (it) {
progressBar.visibility = View.VISIBLE
} else {
progressBar.visibility = View.GONE
}
})
}
{lifecycle_here}
show that I need to put a LifecycleOwner on LiveData observe Call.
Solution
When you use BindingAdapter
with LiveData
, you don't put LiveData
as a parameter. Just put livedata's type directly.
@BindingAdapter("app:progressBarObserver")
fun setProgressBarObserver(progressBar: ProgressBar, isVisible: Boolean) {
if (isVisible) {
progressBar.visibility = View.VISIBLE
} else {
progressBar.visibility = View.GONE
}
}
layout_my.xml
<ProgressBar
...
app:progressBarObserver=@{viewModel.progressBarVisibility}
...
MyViewModel.kt
class MyViewModel {
...
val progressBarVisibility = MutableLiveData<Boolean>(true)
...
}
Answered By - Sanlok Lee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.