效果
实现方案
- activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViewPager()
initTabLayout()
}
private fun initViewPager() {
addViews()
viewPager.addOnPageChangeListener(TabLayoutOnPageChangeListener(tabLayout))
setAdapter()
}
private fun addViews() {
val view1 = LayoutInflater.from(this).inflate(R.layout.view_pager_1, null)
val view2 = LayoutInflater.from(this).inflate(R.layout.view_pager_2, null)
viewPager.addView(view1)
viewPager.addView(view2)
}
private fun setAdapter() {
viewPager.adapter = object : PagerAdapter() {
override fun getCount(): Int {
return viewPager.childCount
}
override fun isViewFromObject(view: View, obj: Any): Boolean {
return view === obj
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
return viewPager.getChildAt(position)
}
}
}
private fun initTabLayout() {
tabLayout.addOnTabSelectedListener(object : BaseOnTabSelectedListener<TabLayout.Tab> {
override fun onTabSelected(tab: TabLayout.Tab?) {
val position = tab!!.position
viewPager.currentItem = position
}
override fun onTabReselected(p0: TabLayout.Tab?) {
}
override fun onTabUnselected(p0: TabLayout.Tab?) {
}
})
}
}
- 布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1"
android:textAllCaps="false" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2"
android:textAllCaps="false" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
view_pager_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="tab1页面"
android:textSize="30sp" />
</LinearLayout>
view_pager_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="tab2页面"
android:textSize="30sp" />
</LinearLayout>