Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass value to viewPagerAdapter?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
            }
        }
    }
}

>Solution :

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()
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading