Tablayout当需要展示除去Title以外的信息的时候这时候就不能重写PagerAdapter的setPagerTitle方法了。这样达不到我们的需求。
此时就需要这样来指定我们的Tablayoutd View
tabCustomView = layoutInflater.inflate(R.layout.layout_tablayout_custom_view, null, false)
tablayout.setupWithViewPager(activity_fit_receipt_viewpager)
tablayout.getTabAt(0).nN().text = "仓库发出"
tablayout.getTabAt(1).nN().customView = tabCustomView
复制代码
activity_fit_receipt_tablayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabReselected(p0: TabLayout.Tab?) {
}
override fun onTabUnselected(p0: TabLayout.Tab?) {
if (p0!!.customView != null && tabCustomView != null) {
tabCustomView!!.find<TextView>(R.id.tv_tab_title).textColor = ContextCompat.getColor(this@FitReceiptListDispatchActivity, R.color.color_gray3)
}
}
override fun onTabSelected(p0: TabLayout.Tab?) {
if (p0!!.customView != null && tabCustomView != null) {
tabCustomView!!.find<TextView>(R.id.tv_tab_title).textColor = ContextCompat.getColor(this@FitReceiptListDispatchActivity, R.color.color_white)
}
}
})复制代码
在监听的时候去指定小圆圈的颜色以及text
并且除了要使用customView之外,tab信息的初始化也一定要放在setUpWithViewPager之后再来执行。因为翻下Tablayout的源码就知道了。
就这样。。。