Adapter with fragments

Report a typo

You have a FragmentStateAdapter that is defined as follows:

class TwoFragmentAdapter(fm: FragmentManager, lifecycle: Lifecycle) : FragmentStateAdapter(fm, lifecycle) {
    override fun getItemCount(): Int {
        return 2
    }

    override fun createFragment(position: Int): Fragment {
        return when (position) {
            0 -> FirstFragment()
            1 -> SecondFragment()
            else -> throw AssertionError()
        }
    }
}

Write the missing line in order to assign the adapter. You are provided with supportFragmentManager and lifecycle in the scope.

val viewPager2 = findViewById<ViewPager2>(R.id.viewPagerContainer)
val pagerAdapter = //Something is missing here!
viewPager2.adapter = pagerAdapter
Enter a short text
___

Create a free account to access the full topic