Android Bottomnavigationbar Bottom Navigation Controller use method detailed _android

Source: Internet
Author: User

Google recently added bottom navigation navigation control to its own material design. Android has no official navigation controller, its own implementation is indeed a variety of, with this rule, similar to Apple's bottom toolbar, the future of our app will have a consistent style, first look at an effect:

This is the official in the material design to give a picture, indeed very good.

1.BottomNavigationBar Download Address

Https://github.com/Ashok-Varma/BottomNavigation

2. Method of Use

2.1 Add in Gradle

Compile ' com.ashokvarma.android:bottom-navigation-bar:0.9.5 '

2.2 Layout implementation

 <com.ashokvarma.bottomnavigation.bottomnavigationbar
    android:layout_gravity= "Bottom"
    android:id= "@+ Id/bottom_navigation_bar "
    android:layout_width=" match_parent "
    android:layout_height=" Wrap_content " >

Add Bottomnavigationitem to activity in class 2.3

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) {
      }
    });

3. Implementation of the case

Layout file

 <?xml version= "1.0" encoding= "Utf-8"?> <relativelayout "xmlns:android=" /schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "xmlns:app=" http:// Schemas.android.com/apk/res-auto "android:layout_width=" match_parent "android:layout_height=" Match_parent "app:
  Layout_behavior= "@string/appbar_scrolling_view_behavior" tools:showin= "@layout/activity_navigation_view_demo" Tools:context= "Com.lidong.demo.navigation_view.
     Bottomnavigationbardemoactivity "> <linearlayout android:id=" @+id/tb "android:layout_width=" Match_parent " android:layout_height= "match_parent" android:orientation= "vertical"/> <com.ashokvarma.bottomnavigation.b Ottomnavigationbar android:id= "@+id/bottom_navigation_bar" android:layout_width= "Match_parent" Android:layout_ Alignparentbottom= "true" android:layout_height= "Wrap_content"/> </RelativeLayout> 

The code for the activity:

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 class Bottomnavigationbardemoactivity extends appcompatactivity implements Bottomn
  Avigationbar.ontabselectedlistener {private Bottomnavigationbar bottomnavigationbar;
  int lastselectedposition = 0;
  Private String TAG = BottomNavigationBarDemoActivity.class.getSimpleName ();
  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"). Setactivecolor (R. Color.orange)). AddItem (New Bottomnavigationitem (R.MIPMAP.IC_FIND_REPLACE_WHITE_24DP, "discovery"). Setactivecolor (R.colo
        R.blue)). AddItem (New Bottomnavigationitem (R.MIPMAP.IC_FAVORITE_WHITE_24DP, "hobby"). Setactivecolor (R.color.green)) . AddItem (New Bottomnavigationitem (R.MIPMAP.IC_BOOK_WHITE_24DP, "book"). Setactivecolor (R.color.blue)). Setfirs

    Tselectedposition (lastselectedposition). Initialise ();
    Bottomnavigationbar.settabselectedlistener (this);
  Setdefaultfragment (); /** * Set the 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 ();
    Open transaction Fragmenttransaction transaction = Fm.begintransaction (); Switch (position) {case 0:if (mlocationfragment = = null) {mlocationfragment = 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.newinstanCE ("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 submitted to Transaction.commit (); @Override public void ontabunselected (int position) {LOG.D (TAG, "ontabunselected () called with:" + "position
  = ["+ position +"]);
 @Override public void ontabreselected (int position) {}}

Fragment 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 class Locationfragment extends Fragment {public static locationfragment newinstance (String param1) {Locat
    Ionfragment fragment = new Locationfragment ();
    Bundle args = new Bundle ();
    Args.putstring ("Agrs1", param1);
    Fragment.setarguments (args);
  return fragment; Public locationfragment () {} @Override the public void OnCreate (Bundle savedinstancestate) {super.oncreate (
  Savedinstancestate); @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedinstanc
    Estate) {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;
 }



}

Code Download: https://github.com/lidong1665/AndroidRapidLibrary

The code is simple to implement, load the layout, add Bottomnavigationitem, set the Tabchangelistener on these three steps to fix the bottom navigation controller.

The above is about Bottomnavigationbar bottom navigation Controller, hope to be helpful to everybody's study.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.