刚开始接触安卓,想用使用tab实现切换,最终使用了ViewPager2和TabLayout实现了Tab切换
在build.gradle.ts的dependesies添加依赖
implementation ("com.google.android.material:material:1.5.0")
implementation ("androidx.viewpager2:viewpager2:1.0.0")
创建Fragment,首页需要五个内容,所以我创建了Fragment
首页布局设置
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IndexActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="411dp"
android:layout_height="657dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@+id/tabLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
</androidx.viewpager2.widget.ViewPager2>
</androidx.constraintlayout.widget.ConstraintLayout>
创建一个适配器类,用于返回Fragment视图到ViewPager里面的
class ViewPagerAdapter(fragmentActivity: FragmentActivity, private var totalCount: Int) :
FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int {
return totalCount
}
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> FindFragment()
1 -> CircleFragment()
2->MyHouseFragment()
3->MessageFragment()
4->MineFragment()
else->FindFragment()
}
}
}
在首页的activity里面创建tab标题及内容
class IndexActivity : AppCompatActivity() {
private lateinit var binding: ActivityIndexBinding
override fun onCreate(savedInstanceState: Bundle?) {
binding=ActivityIndexBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding.root)
setupViewPager()
setupTabLayout()
// tab.
}
val arr= arrayOf("找房","圈子","我的房子","消息","个人中心")
//tabs标题生成
private fun setupTabLayout() {
TabLayoutMediator(
binding.tabLayout, binding.viewPager
) {
tab,
position ->
tab.text = arr[position]
}.attach()
}
//tabs内容生成
private fun setupViewPager() {
val adapter = ViewPagerAdapter(this, 5)
binding.viewPager.adapter = adapter
}
override fun onBackPressed() {
val viewPager = binding.viewPager
if (viewPager.currentItem == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed()
} else {
// Otherwise, select the previous step.
viewPager.currentItem = viewPager.currentItem - 1
}
}
fun clickSwitch(){
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light">
<TextView
android:id="@+id/find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="找房"
/>
</androidx.constraintlayout.widget.ConstraintLayout>