首页 > 解决方案 > 在 Recycler View View-Holder 中暂停函数

问题描述

我在 RecyclerView View Holder 中使用 countdownTimer 函数。由于 whih 应用程序不断停止,此功能给我的主标题带来了太多负担。我怎样才能让这个函数挂起,以便它可以在其他线程上运行。我使用了 Coroutines Scope Dispatches main 但它不起作用。代码附在下面。

  private fun timer(
        millisInFuture: Long,
        countDownInterval: Long
    ): CountDownTimer? {
        countDownTimer = (object : CountDownTimer(millisInFuture, countDownInterval) {
            override fun onTick(millisUntilFinished: Long) {
                val timeRemaining = timeString(millisUntilFinished)
                binding.btnAuctionTimer.visibility = View.VISIBLE
                binding.btnAuctionTimer.text = timeRemaining
            }

            override fun onFinish() {
                binding.btnAuctionTimer.visibility = View.INVISIBLE

            }
        }).start()
        return countDownTimer
    }

标签: androidandroid-recyclerviewkotlin-coroutinescountdowntimersuspend

解决方案


推荐阅读