A tab bar controller to manage navigation using fragments.
class MyActivity : Activity(), TabBarController.Delegate {
val mTabBarController: TabBarController()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mTabBarView.listener = tabBarController
}
}
private fun initTabBarController() {
mHomeFragment = HomeFragment()
mOtherFragment = OtherFragment()
mTabBarController = TabBarController(
rootFragments(),
supportFragmentManager,
R.id.fragmentContainer)
mTabBarController?.delegate = this
mTabBarView.listener = mTabBarController
tabBarControllerDidShowFragment(homeFragment)
}
private fun rootFragments(): List<Fragment> {
return listOf(
mHomeFragment,
mOtherFragment)
}
class TabBarView(context: Context) : LinearLayout(context) {
var listener: TabBarViewListener? = null
enum class TabBarState(val index: Int) {
HOME(0),
PROFILE(1)
}
//In the activity/fragment
tabBarController.switchTab(tabState.index)
tabBarController.pushFragment(fragment)
override fun onBackPressed() {
if (tabBarController != null && !tabBarController!!.back()) {
super.onBackPressed()
}
}