布局同之前的viewpage一样设置就好
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
设置adapter
viewPager.adapter = object : FragmentStateAdapter(requireActivity()) {
override fun getItemCount(): Int {
return 3
}
override fun createFragment(position: Int): Fragment {
return RecordItemFragment.instance(position)
}
}
把tablayout和viewpager2联系到一起
TabLayoutMediator(tabLayout, viewPager) { tab, positon->
//这里可以设置tablayout的属性
tab.text = when(positon) {
0 -> "全部"
1 -> "转出"
2 -> "转入"
else -> ""
}
}.attach()
|