TabLayout tabs text not displaying

后端 未结 4 760
礼貌的吻别
礼貌的吻别 2020-12-08 13:05

I am using TabLayout inside a Fragment to display three fixed tabs and the tabs are working but it doesn\'t show the tab text even after I set the

4条回答
  •  孤街浪徒
    2020-12-08 13:12

    I'm using Kotlin and have spent an hour to find the bugs with this code.

    var viewPager: ViewPager = view.findViewById(R.id.viewPager)
    
    ...
    
    adapter = TabAdapter(fragmentManager!!)
    adapter.addFragment(Fragment1(), "Fragment 1")
    adapter.addFragment(Fragment2(), "Fragment 2")
    
    viewPager.adapter = adapter
    
    tabLayout.setupWithViewPager(viewpager)
    

    I can compile this with no error, but the tab title is not showing until I realize the viewpager I assigned to the tabLayout is not viewPager (notice the capitalize 'P'). It's not producing error because kotlin will find any matching layout id, in this case "viewpager" which is a ViewPager but on another view.

提交回复
热议问题