我正在制作一个函数,当我触摸 recyclerView 项目时,该函数会打开一个对话框。我将 viewModel 放在适配器中,将 onClick 函数放在 viewHolder 中,以使用 viewModel 打开对话框。
\n看起来像这样。
\n Adapter(viewModel) // Initialize adapter in Activity or ViewModel
\xe2\x86\x93
\nclass Adapter(viewModel: ViewModel) : ViewModel() {\n inner class ViewHolder() {\n fun onClick(binding: RecyclerViewItemBinding) : RecyclerView.ViewHolder(binding.root) {\n Dialog(viewModel) // Open Dialog with viewModel\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n我可以在适配器或对话框中使用 ViewModel 吗?如果不能,我该怎么办?我需要通过从对话框接收更改后的值来更改 ViewModel 的数据。
\n