繁体   English   中英

为什么我不能在 Kotlin 的内部类中使用外部属性?

[英]Why can't I use outer property in inner class in Kotlin?

VoiceViewHolder类是VoiceAdapters的内部类。

private val aHomeViewModel是外部类VoiceAdapters的属性。

我认为内部类VoiceViewHolder aHomeViewModel.delete(binding.amVoice)会正确运行,但实际上它失败了,为什么?

代码

class VoiceAdapters (private val aHomeViewModel: HomeViewModel):
        ListAdapter<MVoice, VoiceAdapters.VoiceViewHolder>(MVoiceDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VoiceViewHolder {
        return VoiceViewHolder(
            LayoutVoiceItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun onBindViewHolder(holder: VoiceViewHolder, position: Int) {
        val aMVoice = getItem(position)
        holder.bind(aHomeViewModel, aMVoice)
    }

    class VoiceViewHolder (private val binding: LayoutVoiceItemBinding):
          RecyclerView.ViewHolder(binding.root) {

        init {
            setControl()
        }                

        fun bind(bHomeViewModel: HomeViewModel, aMVoice: MVoice){
            binding.amVoice=aMVoice
            binding.aHomeViewModel=bHomeViewModel
            binding.executePendingBindings()
        }

        fun setControl(){
            binding.btnDelete.setOnClickListener {
                aHomeViewModel.delete(binding.amVoice) //it's error
            }
        }
    }
}


class MVoiceDiffCallback : DiffUtil.ItemCallback<MVoice>() {
   ...
}

希望能帮到你

inner class VoiceViewHolder { ... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM