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
,但这不会影响对false的可见性。
答案 0 :(得分:0)
每当更改executePendingBindings()
的值时,您都必须手动调用isLoading
。
或者您可以使用自定义绑定适配器来实现此功能:
@BindingAdapter("mutableVisibility")
fun setMutableVisibility(view: View, visibility: MutableLiveData<Boolean>?) {
val parentActivity: AppCompatActivity? = view.getParentActivity()
if (parentActivity != null && visibility != null) {
visibility.observe(parentActivity, Observer { value -> view.visibility = if(value) View.VISIBLE else View.GONE})
}
}
然后在您的布局中:
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/clp_fetching"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="40dp"
android:layout_height="40dp"
app:mutableVisibility="@{viewmodel.isLoading}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
答案 1 :(得分:-1)
关于ViewModel用户的一些观察
您正在尝试从“活动”中更新视图模型中的属性,而不应该这样做。
查看模型
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()
}
// This is where you the content of the list would be received
fun onListUpdated(contentList : List<Content>){
businessList.value = contentList;
isLoading.value = false;
}
活动
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 -> // Update UI Here })