tabLayout代码设置选中

单独tabLayout,不与viewpage共用,代码设置选中

     tabLayout.selectTab(tabLayout.getTabAt(0))
    //设置点击 selectTab也会重新走onTabSelected
     tabLayout.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                viewModel?.selectValue.value = tab?.position
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                LogUtils.e("onTabReselected---${tab?.position}")
            }

        })

如果你和viewpage共用,百度到的用法是

  myViewPager.setCurrentItem(position, true);
  tabLayout.getChildAt(0).isSelected=true


这两个都要设置

你可能感兴趣的:(tabLayout代码设置选中)