我有一个带有播客 RecyclerView 的屏幕,当您单击其中一个时,它会将您带到该特定播客的详细信息屏幕。使用 Kodein 进行 ViewModel 注入,我如何将从列表片段中单击的播客的 id 传递给详细片段的 ViewModel,以便可以从 API 中获取它?
详细 ViewModel 的结构如下:
class PodcastDetailViewModel internal constructor(
private val podcastRepository: PodcastRepository,
podcastId: String = ""
): ViewModel() {
// viewmodel stuff
}
细节片段如下所示:
class PodcastDetailFragment : ScopedFragment(), KodeinAware {
override val kodein by closestKodein()
private val args: PodcastDetailFragmentArgs by navArgs()
private val viewModelFactory: PodcastDetailViewModelFactory by kodein.newInstance { PodcastDetailViewModelFactory(args.podcastId, instance()) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = viewModelFactory.create(PodcastDetailViewModel::class.java)
}
// other stuff
}
这就是我从列表中导航到详细信息屏幕的方式:
private fun navigateToPodcastDetailFragment(podcastId: String) {
val args = Bundle()
args.putString("podcast_id", podcastId)
val directions =
TopPodcastsFragmentDirections.viewPodcastDetails(
podcastId
)
val extras = FragmentNavigatorExtras(
podcast_image to "podcastImage_$podcastId"
)
Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
.navigate(directions, extras)
}
这就是我绑定它的方式:
bind() from provider { PodcastDetailViewModelFactory(instance(), instance()) }
我不确定如何在 ViewModelFactory 构造函数中绑定该字符串参数,或者如何在那里传递数据,因此非常感谢任何帮助。