我有一个包含播客列表的RecyclerView屏幕,当您点击其中一个时,它会带您进入该特定播客的详细信息屏幕。使用Kodein进行ViewModel注入,我如何将从列表片段中单击的播客ID传递给详细信息片段的ViewModel,以便可以从API中获取它?
详细ViewModel的结构如下:
细节片段如下:
详细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构造函数中,或者如何传递数据,请帮忙解决。