Skip to content

Latest commit

 

History

History
85 lines (64 loc) · 2.08 KB

README.md

File metadata and controls

85 lines (64 loc) · 2.08 KB

Tab bar

Platform: Android 8+ Language: Kotlin codebeat badge License

A tab bar controller to manage navigation using fragments.

How to start

Link the view with the controller

In the activity

class MyActivity : Activity(), TabBarController.Delegate {  
    val mTabBarController: TabBarController()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mTabBarView.listener = tabBarController
    }
}
Exemple of initialization
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)
    }

In the view

class TabBarView(context: Context) : LinearLayout(context) {

    var listener: TabBarViewListener? = null

How to use it

To switch tab

enum class TabBarState(val index: Int) {
    HOME(0),
    PROFILE(1)
}

//In the activity/fragment
tabBarController.switchTab(tabState.index)

To switch fragment

tabBarController.pushFragment(fragment)

To manage the back button

override fun onBackPressed() {
        if (tabBarController != null && !tabBarController!!.back()) {
            super.onBackPressed()
        }
}