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內(nèi)部自己會管理已實例化的fragment對象,所以不需要考慮復(fù)用的問題.
return fragments[position]
}
}
//以下兩種方式都可以,如果tab的點擊監(jiān)聽不想用系統(tǒng)的眯漩,而是想通過自定義的tabView來實現(xiàn)點擊事件沟优,
//則把下面兩種方式都注釋掉孕蝉,并且通過tabLayout.clearOnTabSelectedListeners()方法清掉tab的點擊監(jiān)聽饼丘,然后使用tabView.setOnClickListener來實現(xiàn),如下所示
//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)
//第二個參數(shù)表示去除滑動效果
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
}
//要執(zhí)行這一句才是真正將兩者綁定起來
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, "復(fù)選的" + tab.text, Toast.LENGTH_SHORT).show()
}
})
}
private fun tabLayoutIndex(position:Int){
//可以來設(shè)置選中時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顯示數(shù)據(jù),只需要一小塊地方進(jìn)行內(nèi)容展示時祈噪,使用tabLayout+recyclerView實現(xiàn)內(nèi)容的切換
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 {
//請求數(shù)據(jù)
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("暫無數(shù)據(jù)")
setEmptyView(emptyView)
setOnItemClickListener { adapter, view, position ->
}
}
recyclerView.adapter=mAdapter
//請求默認(rèn)第一頁數(shù)據(jù)
tabLayoutIndex(0)
refreshData(0)
}
}