android - 当 Fragment 恢复时隐藏 progressBar

标签 android mvvm kotlin viewmodel

我在 ViewModel 类中有这段代码,用于在加载数据时显示进度条:

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>

问题:当我们恢复 fragment 时。它显示 ProgressBar 并且可见性没有像预期的那样消失。可能是什么原因?

还有我的 fragment :

@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 = this@DetailFragment.goal
            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/

相关文章:

android - 在 Android Wear 中打开表盘选择器的 Intent

c# - 如何使用 IDataErrorInfo 从 ViewModel 强制更新 View 的验证错误?

spring-mvc - 具有容器元素约束的Kotlin数据类和bean验证

java - 如何在 Android 中的 Fragment 上使用 OnClickListener

android - 无法签署apk,显示错误如下

java - ConstraintLayout 子项在父 View 中未正确对齐

ios - 保留循环 Swift 闭包

c# - MVVM - 模型与 ViewModel - 将我的代码放在哪里?

android - Realm 对象未更新

kotlin - 重载解析度不明确。所有这些功能都匹配