Android Studio 选项卡布局ViewPager2当点击选项卡号>3时转到最后一页

anhgbhbe  于 2023-11-21  发布在  Android
关注(0)|答案(1)|浏览(172)

我正在实现ViewPager2 + TabLayout。在Java中,类似的代码工作正常,但在Kotlin我有一个问题。当我选择大于3的Tab时,viewPager2将页面(片段)设置在最后一个位置。在viewpager中滑动页面显示正确的页面,同时选择Tablayout中的第1,第2和第3个Tab显示导航到正确的页面。
我已经把一个日志在适配器检查的立场,它打印所有的立场一个接一个。
适配器类

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)

    }
}

字符串
我设置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()

    }


日志

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

62o28rlo

62o28rlo1#

TabLayoutMediator(viewbinding.fragmentTabLayout,viewbinding.tabPage,true,false){ tab,position -> tab.text = FragmentNameList[position] }.attach()
将平滑滚动设置为false可以解决这个问题。

相关问题