TabLayout - ViewPager2

47 views Asked by At

I had to make a TabLayout in a section of my project, and used the viewPager2. I tried to use it like this and it worked but I don't think it is a good practice to do it like this, I have a nav host fragment in the main activity and I am hiding it when using the fragment that contains the TabLayout

    tabLayout = findViewById(R.id.tab_layout)
    viewPager = findViewById(R.id.pager)

    viewPager.adapter = ViewPagerAdapter(this)
    TabLayoutMediator(tabLayout, viewPager) { tab, position ->
        when (position) {
            0 -> tab.text = "Quiz"
            1 -> tab.text = "Ranking"
        }
    }.attach()


    val navHostFragment =
        supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    navController = navHostFragment.navController

    binding.bottomNavigationView.setupWithNavController(navController)

     R.id.quizFragment -> {
                binding.navHostFragment.hide()
                binding.tabLayout.show()
                binding.pager.show()

            }
0

There are 0 answers