Home > Net >  How to pass value to viewPagerAdapter?
How to pass value to viewPagerAdapter?

Time:03-04

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

CodePudding user response:

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