【发布时间】:2020-05-28 08:58:30
【问题描述】:
我的布局中有一个使用数据绑定的 ProgressBar:
<ProgressBar
android:id="@+id/pb_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/sov_avatar"
app:layout_constraintVertical_bias="0.32999998"
app:ui_state_loading="@{viewModel.UIState}"/>
绑定适配器如下所示:
@BindingAdapter("ui_state_loading")
internal fun setUIStateForLoadingContent(view: View, repositoryResult: RepositoryResult<*>) {
Timber.tag("PROGRESS_BAR").d("binding adapter thread: ${Thread.currentThread().name}")
view.visibility = when (repositoryResult) {
is RepositoryResult.Loading -> View.VISIBLE
else -> View.INVISIBLE
}
}
绑定类是这样创建的:
_binding = FragmentRoutesBinding.inflate(layoutInflater, container, false).apply {
lifecycleOwner = this@RoutesFragment
viewModel = mViewModel
UIState 是在我的 ViewModel 中创建的 LiveData:
private val _UIState: MutableLiveData<RepositoryResult<CityData>> by lazy {
MutableLiveData<RepositoryResult<CityData>>().also {
it.value = RepositoryResult.Loading()
viewModelScope.launch {
val repositoryResult = withContext(Dispatchers.IO) {
mRepository.getCityData().run {
val cityData = this.data!!
routesFromJson = cityData.routes
poisFromJson = cityData.pointsOfInterest
// Code to get all the pois to copy in SearchSuggestionsContentProvider
/*val depoList = poisFromJson
var pois: String = ""
depoList?.forEach {
pois += "\"${it.name}\","
}
Timber.d(pois)*/
this
}
}
it.value = repositoryResult
}
}
}
override val UIState: LiveData<RepositoryResult<CityData>> = _UIState
现在,调试我的应用程序,我看到绑定适配器方法被正确调用了 2 次(都在主线程中),并且覆盖了 2 个选项(首先可见,然后不可见,两者之间有几百毫秒),但 ProgressBar 在屏幕上仍然可见。 我试图用 ImageView 替换 ProgressBar,但问题仍然存在。 我不知道是什么原因。也许我改变可见性太快了?
更新 我尝试使用 TextView:更改文本有效,更改可见性不...
更新2 我解决了更改视图的 alpha 值的问题。但是我不知道为什么更改可见性属性不起作用。
【问题讨论】:
-
您是否将进度条声明为约束布局的一部分
Group? -
ProgressBar 在 MotionLayout 中(在运动场景中没有对 ProgressBar 的引用)。
标签: android android-progressbar