前提,SlidingTabLayout的引用和基本用法参考下面链接
Android——可修改字体大小和指示器长度的XTabLayout_KennorWooo的博客-CSDN博客
首先,在xml中:
<com.flyco.tablayout.SlidingTabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/px44dp"
android:background="@color/white"
app:tl_indicator_color="@color/color_2F6CFF"
app:tl_indicator_height="@dimen/px4dp"
app:tl_indicator_width_equal_title="false"
app:tl_tab_space_equal="true"
app:tl_textBold="BOTH"
app:tl_textSelectColor="@color/color_2F6CFF"
app:tl_textUnselectColor="@color/color_33"
app:tl_textsize="@dimen/px15sp" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_f5" />
随后在acticity中:
//先声明
private val mTitles = arrayOf("0", "1")
var fragments: ArrayList<Fragment> = ArrayList()
//然后初始化
fragments.add(ARouterUtils.getFragment(Fragment0, "type", "0"))
fragments.add(ARouterUtils.getFragment(Fragment1, "type", "1"))
val adapter = ViewPagerAdapter(supportFragmentManager, fragments)
vp_main.adapter = adapter
vp_main.offscreenPageLimit = fragments.size
tab_layout.setViewPager(vp_main, mTitles)
tab_layout.showMsg(0, 5)
UnreadMsgUtils.setSize(tab_layout.getMsgView(0), 20.pt.toInt())
tab_layout.setMsgMargin(0, 28.pt, 4.pt)
在Fragment中拿到传过来的type,随后即可在Fragment中处理代码了
@Autowired(name = "type")
@JvmField
var type: String = "0" //