class MainActivity : AppCompatActivity() {
//TODO 初始化Fragment
val sy = FragmentSY.newInstance()
val yy = FragmentYY.newInstance()
val wd = FragmentWD.newInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initBottom()
//TODO 默认显示syFragment
supportFragmentManager.beginTransaction().replace( R.id.frame_id , sy ).commit()
}
//TODO 底部导航
private fun initBottom() {
val titles = arrayOf("首页","影院","我的")
val imgN = arrayOf(R.drawable.home_n,R.drawable.cinima_n,R.drawable.account_n)
val imgP = arrayOf(R.drawable.home_p,R.drawable.cinima_p,R.drawable.account_p)
for (postion in 0..2){
if (postion==0){
tab_id.addTab(tab_id.newTab().setIcon(imgP[postion]).setText(titles[postion]))
}else{
tab_id.addTab(tab_id.newTab().setIcon(imgN[postion]).setText(titles[postion]))
}
}
//TODO 给tab设置选中监听
tab_id.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
override fun onTabReselected(p0: TabLayout.Tab?) {
}
override fun onTabUnselected(p0: TabLayout.Tab?) {
p0?.apply {
setIcon(imgN[position])
}
}
override fun onTabSelected(p0: TabLayout.Tab?) {
p0?.apply {
setIcon(imgP[position])
when(position){
0 -> supportFragmentManager.beginTransaction().replace(R.id.frame_id,sy).commit()
1 -> supportFragmentManager.beginTransaction().replace(R.id.frame_id,yy).commit()
2 -> supportFragmentManager.beginTransaction().replace(R.id.frame_id,wd).commit()
}
}
}
})
}
}