Recently, Google has added bottom navigation navigation controls to its own material design. Android has no official navigation controller, its own implementation is indeed a variety of, with this rule, like Apple's bottom toolbar, after our app will have a consistent style, first look at the effect:
This is the official in material design to give a picture, indeed very good.
Https://github.com/Ashok-Varma/BottomNavigation
2.1 Adding in Gradle
Compile ' com.ashokvarma.android:bottom-navigation-bar:0.9.5 '
2.2 Layout implementation
BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar)bottomNavigationBar .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home")) .addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "Books")) .addItem(new BottomNavigationItem(R.drawable.ic_music_note_white_24dp, "Music")) .addItem(new BottomNavigationItem(R.drawable.ic_tv_white_24dp, "Movies & TV")) .addItem(new BottomNavigationItem(R.drawable.ic_videogame_asset_white_24dp, "Games")) .initialise()
2.4 Setting up event listeners Tabchangelistener
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){ @Override public void onTabSelected(int position) { } @Override public void onTabUnselected(int position) {] } @Override public void onTabReselected(int position) { } });
Layout file
Activity's Code:
Package Com.lidong.demo.navigation_view;import Android.app.fragmentmanager;import Android.app.FragmentTransaction ; Import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.support.v7.widget.toolbar;import Android.util.log;import Com.ashokvarma.bottomnavigation.bottomnavigationbar;import Com.ashokvarma.bottomnavigation.BottomNavigationItem ; Import com.lidong.demo.r;/** * Bottomnavigationbar implementation */public > Bottomnavigationbardemoactivity extends Appcompatactivity implements Bottomnavigationbar.ontabselectedlistener {private Bottomnavigationbar Bottomnavigationbar; int lastselectedposition = 0; Private String TAG = Bottomnavigationbardemoactivity.>private locationfragment mlocationfragment; Private Findfragment mfindfragment; Private Favoritesfragment mfavoritesfragment; Private Bookfragment mbookfragment; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview(R.layout.activity_navigation_view_demo); Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar); Setsupportactionbar (toolbar); Bottomnavigationbar = (Bottomnavigationbar) Findviewbyid (R.id.bottom_navigation_bar); Bottomnavigationbar. AddItem (New Bottomnavigationitem (R.MIPMAP.IC_LOCATION_ON_WHITE_24DP, "location"). Setactiveco Lor (R.color.orange)). AddItem (New Bottomnavigationitem (R.MIPMAP.IC_FIND_REPLACE_WHITE_24DP, "discovery"). setActive Color (R.color.blue)). AddItem (New Bottomnavigationitem (R.MIPMAP.IC_FAVORITE_WHITE_24DP, "hobby"). Setactivecolo R (R.color.green)). AddItem (New Bottomnavigationitem (R.MIPMAP.IC_BOOK_WHITE_24DP, "book"). Setactivecolor (R.col Or.blue). Setfirstselectedposition (lastselectedposition). Initialise (); Bottomnavigationbar.settabselectedlistener (this); Setdefaultfragment (); }/** * Set default */private void Setdefaultfragment () {FraGmentmanager fm = Getfragmentmanager (); Fragmenttransaction transaction = Fm.begintransaction (); Mlocationfragment = locationfragment.newinstance ("position"); Transaction.replace (R.id.tabs, mlocationfragment); Transaction.commit (); } @Override public void ontabselected (int. position) {LOG.D (TAG, "ontabselected () called with:" + "position = ["+ Position +"] "); Fragmentmanager fm = This.getfragmentmanager (); Fragmenttransaction transaction = Fm.begintransaction (); Switch (position) {case 0:if (mlocationfragment = = null) {Mlocationfragmen t = locationfragment.newinstance ("position"); } transaction.replace (R.ID.TB, mlocationfragment); Break Case 1:if (mfindfragment = = null) {mfindfragment = findfragment.newinstance ("discovery"); } transaction.replace (R.ID.TB, MFINDFRAgment); Break Case 2:if (mfavoritesfragment = = null) {mfavoritesfragment = Favoritesfragment.newinst ance ("hobby"); } transaction.replace (R.ID.TB, mfavoritesfragment); Break Case 3:if (mbookfragment = = null) {mbookfragment = Bookfragment.newinstance ("book"); } transaction.replace (R.ID.TB, mbookfragment); Break Default:break; } transaction.commit (); } @Override public void ontabunselected (int. position) {LOG.D (TAG, "ontabunselected () called with:" + "posit Ion = ["+ Position +"] "); } @Override public void ontabreselected (int position) {}}
Fragment's Code
Package Com.lidong.demo.navigation_view;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.TextView ; Import com.lidong.demo.r;public > Locationfragment extends Fragment {public static locationfragment-Newinstance (St Ring param1) {locationfragment fragment = new Locationfragment (); Bundle args = new bundle (); Args.putstring ("Agrs1", param1); Fragment.setarguments (args); return fragment; The public locationfragment () {} @Override the public void OnCreate (Bundle savedinstancestate) {Super.oncre Ate (savedinstancestate); } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundl E savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_location, container, false); Bundle bundle = Getarguments (); String agrs1 = Bundle.getstrING ("agrs1"); TextView TV = (TextView) View.findviewbyid (r.id.tv_location); Tv.settext (AGRS1); return view; }}
The code is very simple to implement, load the layout, add Bottomnavigationitem, set Tabchangelistener three steps to get the bottom navigation controller.
Code
Effect implementation:
Android Bottomnavigationbar Use of the bottom navigation controller