1.布局文件
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabGravity="start"
app:tabBackground="@android:color/transparent"
app:tabRippleColor="@color/white"
app:tabUnboundedRipple="false"
app:tabMode="scrollable"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabLayout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2.子布局tab_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingHorizontal="8dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<View
android:id="@+id/tv_index"
android:layout_width="20dp"
android:layout_height="6dp"
android:layout_marginTop="-6dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_name"
android:background="@drawable/shape_6ccdff_00a8ff_3r_bg"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/black_333"
android:gravity="center"
android:maxLength="4"
android:textStyle="bold"/>
</androidx.constraintlayout.widget.ConstraintLayout>
3.交互代码
private var mediator: TabLayoutMediator? = null
private val mTitles = arrayOf("全部","待支付","已支付","已换绑","已完成","已取消")
private fun initViewPager() {
val fragments:MutableList<Fragment> = ArrayList()
fragments.add(BuyRecordFragment.newInstance(null))
fragments.add(BuyRecordFragment.newInstance("0"))
fragments.add(BuyRecordFragment.newInstance("1"))
fragments.add(BuyRecordFragment.newInstance("2"))
fragments.add(BuyRecordFragment.newInstance("3"))
fragments.add(BuyRecordFragment.newInstance("4"))
mBinding.apply {
//viewPager.offscreenPageLimit = fragments.size
viewPager.adapter = object : FragmentStateAdapter(supportFragmentManager,lifecycle) {
override fun getItemCount(): Int {
return fragments.size
}
override fun createFragment(position: Int): Fragment {
//FragmentStateAdapter内部自己会管理已实例化的fragment对象,所以不需要考虑复用的问题.
return fragments[position]
}
}
//以下两种方式都可以,如果tab的点击监听不想用系统的,而是想通过自定义的tabView来实现点击事件,
//则把下面两种方式都注释掉,并且通过tabLayout.clearOnTabSelectedListeners()方法清掉tab的点击监听,然后使用tabView.setOnClickListener来实现,如下所示
//viewPager.registerOnPageChangeCallback(changeCallback)//第一种方式
//addTabLayoutListener()//第二种方式
mediator = TabLayoutMediator(tabLayout, viewPager,false,false) { tab, position ->
//这里可以自定义TabView
val tabView = LayoutInflater.from(context).inflate(R.layout.tab_item_layout, null)
//第三种方式
tabView .setOnClickListener {
tabLayoutIndex(position)
//第二个参数表示去除滑动效果
viewPager.setCurrentItem(position,false)
}
val tv_name =tabView .findViewById<TextView>(R.id.tv_name)
val tv_index =tabView .findViewById<View>(R.id.tv_index)
tv_name.text = mTitles[position]
tv_index.visibility=View.INVISIBLE
tab.customView = tabView
}
//要执行这一句才是真正将两者绑定起来
mediator?.attach()
//配合第三种方式时,需要初始化一个item
tabLayout.clearOnTabSelectedListeners()
tabLayoutIndex(0)
viewPager.setCurrentItem(0,false)
}
}
private val changeCallback = object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
tabLayoutIndex(position)
}
}
private fun addTabLayoutListener(){
mBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
tabLayoutIndex(tab.position)
//Toast.makeText(mContext, "选中的" + tab.text, Toast.LENGTH_SHORT).show()
}
override fun onTabUnselected(tab: TabLayout.Tab) {
//Toast.makeText(mContext, "未选中的" + tab.text, Toast.LENGTH_SHORT).show()
}
override fun onTabReselected(tab: TabLayout.Tab) {
//Toast.makeText(mContext, "复选的" + tab.text, Toast.LENGTH_SHORT).show()
}
})
}
private fun tabLayoutIndex(position:Int){
//可以来设置选中时tab的大小
val tabCount: Int = mBinding.tabLayout.tabCount
for (i in 0 until tabCount) {
val tab: TabLayout.Tab = mBinding.tabLayout.getTabAt(i)!!
val tabView = tab.customView
val tv_name = tabView?.findViewById<TextView>(R.id.tv_name)
val tv_index = tabView?.findViewById<View>(R.id.tv_index)
if (tab.position == position) {
if (tv_name != null) {
tv_name.textSize = 18f
}
if (tv_index != null) {
tv_index.visibility=View.VISIBLE
}
} else {
if (tv_name != null) {
tv_name.textSize = 14f
}
if (tv_index != null) {
tv_index.visibility=View.INVISIBLE
}
}
}
}
override fun onDestroy() {
mediator?.detach()
//mBinding.viewPager.unregisterOnPageChangeCallback(changeCallback)
super.onDestroy()
}
//如果页面中不需要fragment显示数据,只需要一小块地方进行内容展示时,使用tabLayout+recyclerView实现内容的切换
private fun setTabData(faqList:MutableList<Faq>){
mBinding.apply {
for (item in faqList){
val tab=tabLayout.newTab()
val view = LayoutInflater.from(mContext).inflate(R.layout.tab_item_layout, null)
val tvName =view.findViewById<TextView>(R.id.tv_name)
val tvIndex =view.findViewById<View>(R.id.tv_index)
tvName.text = item.title
tvIndex.visibility= View.INVISIBLE
tab.customView = view
tabLayout.addTab(tab)
}
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
tab?.position?.let {
//请求数据
tabLayoutIndex(it)
refreshData(it)
}
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabReselected(tab: TabLayout.Tab?) {
}
})
val manager=LinearLayoutManager(mContext)
recyclerView.layoutManager= manager
mAdapter= OnlineServerAdapter()
mAdapter?.apply {
val emptyView = EmptyView(mContext, null)
emptyView.changeEmptyImgTopMargin(0f)
//emptyView.setEmptyIcon(R.mipmap.ic_empty_account_msg)
emptyView.setEmptyDesc("暂无数据")
setEmptyView(emptyView)
setOnItemClickListener { adapter, view, position ->
}
}
recyclerView.adapter=mAdapter
//请求默认第一页数据
tabLayoutIndex(0)
refreshData(0)
}
}