I am implementing ViewPager2 + TabLayout. In Java the similar code worked fine but in Kotlin I am having a problem. when I select Tab greater than 3 the viewpager2 sets the page(Fragment) in the last position. Swiping the page in the viewpager shows the correct page also selecting the 1st,2nd, and 3rd Tab in Tablayout shows navigate to the correct page.
I had put a log in Adapter to check the position it prints all positions one by one.
Adapter Class
class ViewPager2Adapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
FragmentStateAdapter(fragmentManager, lifecycle) {
private val fragmentList: ArrayList<Fragment> = ArrayList<Fragment>()
override fun createFragment(position: Int): Fragment {
Log.e("TAG",position.toString() )
return fragmentList[position]
}
fun addFragment(fragment: Fragment) {
fragmentList.add(fragment)
}
override fun getItemCount(): Int {
return fragmentList.size
}
override fun getItemViewType(position: Int): Int {
return position
}
fun add(s: Fragment) {
fragmentList.add(s)
}
}
Fragment from where i am setting the Viewpager
fun setupTabLayout() {
val home = activity as HomeDash
val myAdapter = ViewPager2Adapter(this.getChildFragmentManager() , this.lifecycle)
var FragmentNameList:Array<String>
myAdapter.addFragment(StatisticsFragment())
myAdapter.addFragment(ApprovalFragment())
myAdapter.addFragment(ProfessionalDetailsFragment())
myAdapter.addFragment(PersonalDetailsFragment())
myAdapter.addFragment(EducationalDetailsFragment())
myAdapter.addFragment(BankDetailsFragmnt())
myAdapter.addFragment(SalaryDetailsFragment())
myAdapter.addFragment(LoanFragment())
FragmentNameList = arrayOf(
"Statistics",
"Approval",
"Professional Details",
"Personal Details",
"Educational Details",
"Bank Details",
"Salary Details",
"Loan Details",
)
viewbinding.tabPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL)
viewbinding.tabPager.setAdapter(myAdapter)
TabLayoutMediator(viewbinding.fragmentTabLayout, viewbinding.tabPager) { tab, position ->
tab.text = FragmentNameList[position]
//viewbinding.tabPager.setCurrentItem(position, false)
}.attach()
}
Log
2023-10-23 17:00:01.125 3752-3752 TAG gen.reporting_mvvm E 0
2023-10-23 17:00:02.534 3752-3752 TAG gen.reporting_mvvm E 1
2023-10-23 17:00:02.550 3752-3752 TAG gen.reporting_mvvm E On create
2023-10-23 17:00:02.876 3752-3764 System gen.reporting_mvvm W A resource failed to call close.
2023-10-23 17:00:02.916 3752-3752 RecyclerView gen.reporting_mvvm W No adapter attached; skipping layout
2023-10-23 17:00:03.183 3752-3752 RecyclerView gen.reporting_mvvm W No adapter attached; skipping layout
2023-10-23 17:00:03.187 3752-3752 View gen.reporting_mvvm D [ANR Warning]onLayout time too long, this =androidx.recyclerview.widget.RecyclerView{9f12e4b VFED..... ......ID 54,48-1026,1579 #7f0a00a1 app:id/RecyclerApproval}time =629 ms
2023-10-23 17:00:03.187 3752-3752 View gen.reporting_mvvm D [ANR Warning]onLayout time too long, this =androidx.constraintlayout.widget.ConstraintLayout{cdc028 V.E...... ......ID 0,0-1080,1611}time =630 ms
2023-10-23 17:00:03.187 3752-3752 View gen.reporting_mvvm D [ANR Warning]onLayout time too long, this =android.widget.FrameLayout{f173141 V.E...... ......ID 1080,0-2160,1611 #3}time =630 ms
2023-10-23 17:00:03.189 3752-3752 TAG gen.reporting_mvvm E 2
2023-10-23 17:00:04.067 3752-3752 Looper gen.reporting_mvvm W PerfMonitor longMsg : seq=1367 plan=17:00:02.518 late=12ms wall=1533ms h=android.view.Choreographer$FrameHandler c=android.view.Choreographer$FrameDisplayEventReceiver procState=-1
2023-10-23 17:00:04.067 3752-3752 Looper gen.reporting_mvvm W PerfMonitor doFrame : time=1533ms vsyncFrame=0 latency=12ms procState=-1 historyMsgCount=2
2023-10-23 17:00:04.071 3752-3752 Choreographer gen.reporting_mvvm I Skipped 138 frames! The application may be doing too much work on its main thread.
2023-10-23 17:00:04.158 3752-3752 TAG gen.reporting_mvvm E 3
2023-10-23 17:00:04.620 3752-3752 TAG gen.reporting_mvvm E 4
2023-10-23 17:00:05.554 3752-3752 TAG gen.reporting_mvvm E 5
2023-10-23 17:00:06.027 3752-3752 TAG gen.reporting_mvvm E 6
2023-10-23 17:00:06.872 3752-3752 TAG gen.reporting_mvvm E 7
TabLayoutMediator(viewbinding.fragmentTabLayout, viewbinding.tabPage,true,false) { tab, position -> tab.text = FragmentNameList[position] }.attach()
Setting smooth scrolling false solves the problem.