Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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

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()
            }
        }
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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()
            }
        }
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!