当前位置:网站首页>TabLayout + ViewPager2 + Fragment的简单应用
TabLayout + ViewPager2 + Fragment的简单应用
2022-04-22 18:46:00 【否定观】
在fragment_container.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout_book"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager_book"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在activity_main中静态加载fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:ignore="MissingConstraints">
<fragment class="com.example.englishstudy.ui.book.BookFragment"
android:id="@+id/dsk"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
在MainActivity文件中
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
再需要弄个适配器,设置TabItem的数目以及viewpager对应要加载的Fragment
class BookAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int {
return 4
}
override fun createFragment(position: Int): Fragment {
return when(position){
0 -> {
BookListFragment()
}
1 -> {
ListenFragment()
}
2 -> {
BookListFragment()
}
else ->{
BookListFragment()
}
}
}
}
最后在Fragment中完成具体的操作
利用 TabLayoutMediator 将TabLayout和ViewPager2之间的联动
利用addOnTabSelectedListener监听TabItem的点击
class BookFragment: Fragment() {
private var _binding: FragmentBookContainerBinding? = null
private val binding
get() = _binding!!
private val TAG = "BookFragment"
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentBookContainerBinding.inflate(layoutInflater,container,false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.viewPagerBook.adapter = BookAdapter(this)
TabLayoutMediator(binding.tabLayoutBook,binding.viewPagerBook) { tab, position ->
when(position){
0 -> tab.text = "阿萨德"
1 -> tab.text = "请王尔德"
2 -> tab.text = "定金付货款"
else -> tab.text = "觉得很疯狂”"
}
}.attach()
binding.tabLayoutBook.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
// 点击
override fun onTabSelected(tab: TabLayout.Tab?) {
logD(TAG,"addOnTabSelectedListener111 ${tab!!.position}")
when(tab.position) {
0 -> tab_num = 0
1 -> tab_num = 1
2 -> tab_num = 2
3 -> tab_num = 3
}
}
// 释放点击
override fun onTabUnselected(tab: TabLayout.Tab?) {
logD(TAG,"addOnTabSelectedListener222 ${tab!!.position} ")
}
override fun onTabReselected(tab: TabLayout.Tab?) {
logD(TAG,"addOnTabSelectedListener333 ")
}
})
}
}
期间出现的问题:尝试在TabLayout下直接添加TabItem,出现 Error inflating class fragment 的错误,具体原因不明
解决方法:去掉TabItem中的 id 即可
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout_book"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_item0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.google.android.material.tabs.TabLayout>
版权声明
本文为[否定观]所创,转载请带上原文链接,感谢
https://blog.csdn.net/boyfriend_soldo/article/details/124259468
边栏推荐
猜你喜欢
随机推荐
Win10问题篇:一次性永久关闭win10系统自动更新
server端密码加密
Recommendation of safe, fast and low-cost futures companies in 2022?
100 Days of Code-day26(年月日转换的奥秘)
图像的卷积——【torch学习笔记】
浅析局域网聊天软件的能力
PCB Layout Stackup setting
JVM composition
sh文件内容启动文件
第119章 SQL函数 RIGHT
带你了解极具弹性的Spark架构的原理
2022年江西省安全员A证考试练习题及模拟考试
2022福建省安全员A证(主要负责人)考试模拟100题及在线模拟考试
模块代码的类型描述文件
Jsonobject data guarantee order of fastjson
视频知识点(16)- 如何将y4m文件转换成yuv文件?
【接口测试基础】第九篇 | 详解PostMan全局变量和环境变量
fastjson的JSONObject数据保证顺序
数据分析师职业规划——数据分析师的职业焦虑与未来发展
Convolution of images -- [torch learning notes]








