如何将值传递给viewPagerAdapter?
根据我的代码,我尝试加载两种不同样式的 viewpager 适配器。我根据 tabItemCount
在此处设置选项卡和位置字符串。我想在 viewpager 适配器内做同样的事情。我刚刚尝试 tabItemCount
到 viewpageradapter
并根据值加载适配器。例如:如果 tabItemCount 为 2,则加载两个选项卡。如果是 3,则加载 3 个选项卡。所以我不必使用两个不同的适配器。但我无法将 tabItemCount 传递给 viewpageradapter。
MainFragment.kt
private fun initAdapters() {
val tabItemCount: Int = if (viewModel.appointmentType == AppointmentType.FromHospitalAppointment) 3 else 2
val adapter = MainViewPagerAdapter(
childFragmentManager, viewLifecycleOwner.lifecycle, tabItemCount
)
binding.apply {
viewPager2.adapter = adapter
TabLayoutMediator(tabLayoutGetAppointmentStepSecond, viewPager2GetAppointmentStepSecond) { tab, position ->
if (tabItemCount == 2) {
when (position) {
0 -> tab.text = getString(R.string.asm_appointments_in_hospital)
1 -> tab.text = getString(R.string.asm_video_appointments)
}
} else {
when (position) {
0 -> tab.text = getString(R.string.asm_appointments_in_hospital)
1 -> tab.text = getString(R.string.asm_video_appointments)
2 -> tab.text = getString(R.string.asm_hospital)
}
}
}.attach()
}
}
MainViewPagerAdapter.kt
class MainViewPagerAdapter(
fragmentManager: FragmentManager, lifecycle: Lifecycle, tabItemCount
) : FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int {
return 3
}
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> {
MyProfileInformationASMFragment()
}
1 -> {
MyProfilePersonInformationASMFragment()
}
2 -> {
MyProfileHealthCardASMFragment()
}
else -> {
Fragment()
}
}
}
}
According to my code I am trying to load viewpager adapter with two different style. I set here tab and position string according to tabItemCount
. I want to do samething inside viewpager adapter. I just tried tabItemCount
to viewpageradapter
and load the adapter according to value. For example: if tabItemCount is 2 load tow tabs. if it is 3 load 3 tabs. So I wil not have to use two different adapter. But I can't pass tabItemCount to viewpageradapter.
MainFragment.kt
private fun initAdapters() {
val tabItemCount: Int = if (viewModel.appointmentType == AppointmentType.FromHospitalAppointment) 3 else 2
val adapter = MainViewPagerAdapter(
childFragmentManager, viewLifecycleOwner.lifecycle, tabItemCount
)
binding.apply {
viewPager2.adapter = adapter
TabLayoutMediator(tabLayoutGetAppointmentStepSecond, viewPager2GetAppointmentStepSecond) { tab, position ->
if (tabItemCount == 2) {
when (position) {
0 -> tab.text = getString(R.string.asm_appointments_in_hospital)
1 -> tab.text = getString(R.string.asm_video_appointments)
}
} else {
when (position) {
0 -> tab.text = getString(R.string.asm_appointments_in_hospital)
1 -> tab.text = getString(R.string.asm_video_appointments)
2 -> tab.text = getString(R.string.asm_hospital)
}
}
}.attach()
}
}
MainViewPagerAdapter.kt
class MainViewPagerAdapter(
fragmentManager: FragmentManager, lifecycle: Lifecycle, tabItemCount
) : FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int {
return 3
}
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> {
MyProfileInformationASMFragment()
}
1 -> {
MyProfilePersonInformationASMFragment()
}
2 -> {
MyProfileHealthCardASMFragment()
}
else -> {
Fragment()
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试像这样更改
MainViewPagerAdapter
类声明Try to change the
MainViewPagerAdapter
class declaration like this