我在ViewModel类中有此代码,以在加载数据时显示progressBar:
class DetailViewModel(
context: Application,
private val schedulerProvider: BaseSchedulerProvider,
private val dataSource: RemoteDataSource
) : AndroidViewModel(context) {
val isFeedsLoading = ObservableBoolean(false)
fun showFeeds(goal: SavingsGoal): Disposable? {
EspressoIdlingResource.increment() // App is busy until further notice
isFeedsLoading.set(true)
return dataSource.getFeeds(goal.id)
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.doFinally {
if (!EspressoIdlingResource.getIdlingResource().isIdleNow) {
EspressoIdlingResource.decrement()
}
isFeedsLoading.set(false)
}
?.subscribe({ feeds ->
}
) {
Timber.e(it)
}
}
和布局:
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
app:items="@{vm.feeds}"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:visibleGone="@{vm.isFeedsLoading}"/>
</FrameLayout>
问题:当我们恢复片段时。它显示ProgressBar,可见性未达到预期。可能是什么原因?
还有我的片段:
@ActivityScoped
class DetailFragment @Inject
constructor() // Required empty public constructor
: DaggerFragment() {
@Inject
lateinit var viewModelFactory: DetailViewModel.DetailViewModelFactory
@Inject
lateinit var goal: SavingsGoal
private val compositeDisposable = CompositeDisposable()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val viewModel = ViewModelProviders.of(this, viewModelFactory)[DetailViewModel::class.java]
val root = inflater.inflate(R.layout.fragment_detail, container, false)
val binding = FragmentDetailBinding.bind(root).apply {
setVariable(BR.vm, viewModel)
goal = [email protected]
lifecycleOwner = this@DetailFragment
}
with(root) {
with(activity as AppCompatActivity) {
setupActionBar(binding.toolbar) {
setDisplayShowTitleEnabled(false)
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}
}
}
binding.vm?.showFeeds(goal)?.let { compositeDisposable.add(it) }
return root
}
override fun onDestroyView() {
super.onDestroyView()
compositeDisposable.clear()
}
}
绑定(bind)适配器:
@BindingAdapter("visibleGone")
fun View.visibleGone(visible: Boolean) {
visibility = if (visible) View.VISIBLE else View.GONE
}
最佳答案
不知道这是您要找的..吗?
android:visibility="@{vm.isFeedsLoading} ? View.VISIBLE : View.GONE"
https://stackoverflow.com/a/47746579/7697633
关于android - fragment 恢复时隐藏progressBar,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55370418/