override fun getItem(position: Int): Fragment {
var mainFragment: Fragment? = null
val first = FirstFragment()
val second = SecondFragment()
val third = ThirdFragment()
when (position) {
0 -> {
mainFragment = first
}
1 -> {
mainFragment = second
}
2 -> {
mainFragment = third
}
}
return mainFragment!!
}
Pager Adapter
Kotlin recipesIn this code, the getItem() method is overriding the default Fragment.getItem() method. The getItem() method is responsible for getting the main fragment from the given position. The first position is 0, the second position is 1, and so on. The when() block checks the position and then calls the appropriate method depending on the position. The return value of the getItem() method is the main fragment.
0 Comments
Add Comment
Log in to add a comment