Skip to content

(Sample 4) Rail Navigation

Kaustubh Patange edited this page Jun 19, 2021 · 2 revisions

The code sets up rail navigation in a fragment (the same can be followed for activity).

class MainFragment : ValueFragment(), FragmentNavigator.Transmitter {
    private lateinit var navigator: FragmentNavigator

    // The host must implement FragmentNavigator.Transmitter interface so
    // that all it's child fragment get the same Navigator instance.
    override fun getNavigator(): FragmentNavigator = navigator

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        ...
        navigator = FragmentNavigator.with(this, savedInstanceState)
            .initialize(binding.container)

        // Actual rail navigation setup. Rest is handled for you including selection change,
        // animations, many more.
        navigator.install(object : FragmentNavigator.RailNavigation(){
            // The @IdRes of the NavigationRailView
            override val railNavigationViewId: Int = R.id.bottom_nav

            // Map of Ids of bottom navigation menu resource item to Fragment.
            //
            // Simply means the menu resource which is set on BottomNavigationView
            // has Id eg: R.id.fragment_home which when selected HomeFragment will
            //  be shown or brought to front.
            override val bottomNavigationFragments: Map<Int, KClass<out Fragment>> =
                mapOf(
                    R.id.fragment_home to HomeFragment::class,
                    R.id.fragment_backup to BackupFragment::class,
                    R.id.fragment_settings to SettingFragment::class,
                )

            // Slide from left/right animation when selection is changed.
            override val fragmentNavigationTransition = Animation.SlideVertically
        })
    }
}
class HomeFragment() : ValueFragment() // or ValueFragment
class BackupFragment() : ValueFragment()
class SettingFragment() : ValueFragment()
Clone this wiki locally