Hysen Labs
Open-source project
jaisonfdo/BottomNavigation avatar
jaisonfdo

BottomNavigation

A sample app for Bottom Navigation View with ViewPager in Android

100 stars46 forksJavaLicense varies
01
DEEP OPEN-SOURCE ANALYSIS

BottomNavigation: tying Android's bottom nav to a pager

A sample Android project that pairs the Bottom Navigation View with a ViewPager. The README lists the steps in order, from the Design Support Library dependency to the two listeners that keep menu and page in sync.

02
DEEP OPEN-SOURCE ANALYSIS

The setup step

The first step is to create an application with the latest Design Support Library, version 25, added to your build.gradle so that BottomNavigationView is available. That dependency is the entry point for the whole sample. The repository is a sample Android app written in Java, and its purpose is to demonstrate how the two components cooperate in a real project. From the dependency, the rest of the README is about wiring the navigation view to a pager.

03
DEEP OPEN-SOURCE ANALYSIS

Wiring the two listeners

Two listeners carry the behavior. First you add an OnNavigationItemSelectedListener to the BottomNavigationView and override OnNavigationItemSelected with the relevant action. Then you add an OnPageChangeListener to the ViewPager and override onPageSelected. The README calls that second step the magic, since the code there selects the relevant item in the BottomNavigationView whenever the page changes. The two listeners are what join the menu and the pager into a single working component.

04
DEEP OPEN-SOURCE ANALYSIS

Keeping state in sync

Inside onPageSelected, the code unchecks the previous menu item, or the first item if there was none, then checks the item at the current position and remembers it. That keeps the checked state aligned with the visible page in both directions. So tapping a tab changes the page, and swiping the pager changes the highlighted tab. For more detail, the README points to a guide at droidmentor.com, which is the author's detailed walkthrough of the same setup.

06
DEEP OPEN-SOURCE ANALYSIS

Official sources

07
Community notes

Community notes