MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityMainBinding = DataBindingUtil.setContentView(this,
R.layout.activity_main)
mainViewModel = ViewModelProviders.of(
this@MainActivity,
mainViewModelFactory
)[MainViewModel::class.java]
activityMainBinding.viewmodel = mainViewModel
activityMainBinding.lifecycleOwner = this
mainViewModel.fetchBusinessList()
mainViewModel.businessList.observe(this, Observer { items ->
run {
mainViewModel.isLoading.value = false
}
})
MainViewModel
class MainViewModel(val repository: MainActivityRepo) : ViewModel() {
val isLoading: MutableLiveData<Boolean> = MutableLiveData()
lateinit var businessList: LiveData<List<Content>>
fun fetchBusinessList() {
isLoading.value = true
businessList = repository.getBusinessList()
}
Макет
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/clp_fetching"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="40dp"
android:layout_height="40dp"
android:visibility="@{viewmodel.isLoading?View.VISIBLE:View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Я пробовал setValue
, postValue
, а также добавил LifeCycleOwner
этоне влияет на видимость ложных.