如何将值传递给viewPagerAdapter?

发布于 2025-01-11 15:38:02 字数 2297 浏览 1 评论 0原文

根据我的代码,我尝试加载两种不同样式的 viewpager 适配器。我根据 tabItemCount 在此处设置选项卡和位置字符串。我想在 viewpager 适配器内做同样的事情。我刚刚尝试 tabItemCountviewpageradapter 并根据值加载适配器。例如:如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2025-01-18 15:38:02

尝试像这样更改 MainViewPagerAdapter 类声明

class MainViewPagerAdapter(
    fragmentManager: FragmentManager, 
    lifecycle: Lifecycle, 
    val tabItemCount: Int
) : FragmentStateAdapter(fragmentManager, lifecycle) {

    override fun getItemCount(): Int = tabItemCount

    override fun createFragment(position: Int): Fragment {
        return when (position) {
            0 -> {
                MyProfileInformationASMFragment()
            }
            1 -> {
                MyProfilePersonInformationASMFragment()
            }

            2 -> {
                MyProfileHealthCardASMFragment()
            }
            else -> {
                Fragment()
            }
        }
    }
}

Try to change the MainViewPagerAdapter class declaration like this

class MainViewPagerAdapter(
    fragmentManager: FragmentManager, 
    lifecycle: Lifecycle, 
    val tabItemCount: Int
) : FragmentStateAdapter(fragmentManager, lifecycle) {

    override fun getItemCount(): Int = tabItemCount

    override fun createFragment(position: Int): Fragment {
        return when (position) {
            0 -> {
                MyProfileInformationASMFragment()
            }
            1 -> {
                MyProfilePersonInformationASMFragment()
            }

            2 -> {
                MyProfileHealthCardASMFragment()
            }
            else -> {
                Fragment()
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文