当前位置:网站首页>Simple application of tablayout + viewpager2 + fragment
Simple application of tablayout + viewpager2 + fragment
2022-04-22 18:48:00 【Negative view】
stay fragment_container.xml in
<?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>
stay activity_main Static loading in 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>
stay MainActivity In file
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
You need to get another adapter , Set up TabItem The number of and viewpager Corresponding to 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()
}
}
}
}
Last in Fragment Complete specific operations in
utilize TabLayoutMediator take TabLayout and ViewPager2 The linkage between
utilize addOnTabSelectedListener monitor TabItem Click on
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 = " Assad "
1 -> tab.text = " Please Wilde "
2 -> tab.text = " Deposit to pay for the goods "
else -> tab.text = " Feel crazy ”"
}
}.attach()
binding.tabLayoutBook.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
// Click on
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
}
}
// Release Click
override fun onTabUnselected(tab: TabLayout.Tab?) {
logD(TAG,"addOnTabSelectedListener222 ${tab!!.position} ")
}
override fun onTabReselected(tab: TabLayout.Tab?) {
logD(TAG,"addOnTabSelectedListener333 ")
}
})
}
}
The problems in the process : Try to TabLayout Next, add TabItem, appear Error inflating class fragment Error of , The specific reason is unknown
resolvent : Get rid of TabItem Medium id that will do
<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>
版权声明
本文为[Negative view]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221846320949.html
边栏推荐
- Transformation operator
- [untitled] 2022 coal mine outburst prevention test exercise and simulation test
- Do you know how many sensors are on your phone?
- Detailed understanding of gaussdb and how to do data migration [play with Huawei cloud]
- 企业沟通软件与其他通信对比的优势有哪些
- redis主从复制
- 对比两个list的差异,list的每个元素是string
- 高可用之——consul 文档
- ul li实现多行多列的布局思路
- readline分析日志
猜你喜欢

Kellerman Software .NET SFTP Library
![[most complete in the whole network] jsr303 parameter verification and global exception handling (don't use if to judge parameters from theory to practice)](/img/43/6b13705902d1768c9fd8cf2968b838.png)
[most complete in the whole network] jsr303 parameter verification and global exception handling (don't use if to judge parameters from theory to practice)

微星小飞机 性能监控设置

Jsonobject data guarantee order of fastjson

jsp学习(九.Filter过滤器、通配符和Cookie处理)

带你了解极具弹性的Spark架构的原理

CDATA drivers for JIRA complete features and attributes

RHCE-ansible

企业沟通软件与其他通信对比的优势有哪些
![[proteus simulation] 51 single chip microcomputer 8-way actuator inching ± 90 ° inching control](/img/ab/d905bd1493f17acd8909d25308a07d.png)
[proteus simulation] 51 single chip microcomputer 8-way actuator inching ± 90 ° inching control
随机推荐
Do you know how many sensors are on your phone?
跨链资产交互——如何在Moonriver上转移KSM
2022年江西省安全员A证考试练习题及模拟考试
数据分析师职业规划——数据分析师的职业焦虑与未来发展
2022年挖掘机司机(建筑特殊工种)复训题库及答案
力扣-64.最小路径和
100 days of code-day26
One click download scheme of serial port of esp32 / esp8266 series single chip microcomputer without peripheral circuit
2022装载机司机(建筑特殊工种)上岗证题目模拟考试平台操作
jsp学习(九.Filter过滤器、通配符和Cookie处理)
stream演示
Flink之转换算子 (Transformation)
基于SSM框架开发OA企业在线办公系统项目教程-附源码-毕业设计
Take you to understand the principle of highly flexible spark architecture
[proteus simulation] simple calculator made by 51 single chip microcomputer + LCD1602 display
Proteus 8.9SP2仿真软件
Pattern machine template computer CAD free pattern drawing and format conversion software ps300b tutorial: general CAD drawing and pattern drawing of Japanese brother pattern machine DXF file transfer
Proteus 8.9sp2 simulation software
nodejs文件操作
Server side password encryption