微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在片段的私有函数中使用 adapterPosition

如何解决在片段的私有函数中使用 adapterPosition

我正在尝试访问

adapterPosition

但是,在私有函数中,出现以下错误

unresolved reference: adapterPosition

它找不到要转到的声明。

当我在这个私有函数之外使用 adapterPosition 时,我没有这个错误

  private fun speakCurrentValue() {


    val position = adapterPosition
    val currentItem = exampleList[position]
    lateinit var tts: TextToSpeech

    tts.speak(
        currentItem.percent.toString(),TextToSpeech.QUEUE_ADD,null,""
    )

    handler.postDelayed(Runnable{speakCurrentValue()},delayMs)
}

有关如何解决错误的任何想法?

解决方法

adapterPosition 将是 Int。您可以将其作为参数传递给您的 private function

 private fun speakCurrentValue(position: Int) {

    val currentItem = exampleList[position]
    lateinit var tts: TextToSpeech

    tts.speak(
        currentItem.percent.toString(),TextToSpeech.QUEUE_ADD,null,""
    )

    handler.postDelayed(Runnable{speakCurrentValue()},delayMs)
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。