Android Navigation tab Bar implementation

Source: Internet
Author: User

Introduction to the Android sliding control a lot, I believe most of the students touch is the listview of this vertical sliding control, but sometimes we also have the need for lateral sliding, many applications are now made this look, take Weiciyuan as an example, as shown, Several tab buttons above can be slid.
Navigation tabs Add Navigation tab bar, is a common design style, I believe you also often see it. android2.x era, General people are used tabhost to achieve (before the project using this design bitter force for a while), since Google introduced design specifications, Google strongly advocated the use of navigation Tabs plus fragment and Viewpager to achieve the navigation page of the click Switch and slide switch. As shown, this is a typical example of navigation tabs:



Implementation code is relatively simple, because it is open source project, I directly paste out the code, convenient for everyone to learn reference.
public class Mainuiactivity extends Fragmentactivity {private static final int cache_pager = 5;private Viewpager mviewpage R;private mainuipageradapter mainuipageradapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {                Super.oncreate (savedinstancestate);                Mviewpager = new Viewpager (this);                Mviewpager.setid (11); Setcontentview (Mviewpager); Buildphoneinterface ();} private void Buildphoneinterface () {Buildviewpager (); Buildactionbarandviewpagertitles (); Getactionbar (). Setselectednavigationitem (1);} private void Buildviewpager () {Mviewpager = (Viewpager) Findviewbyid (r.id.mainviewpager); mainuipageradapter = new Mainuipageradapter (Getfragmentmanager ()); Mviewpager.setoffscreenpagelimit (Cache_pager); MViewPager.setAdapter ( Mainuipageradapter); Mviewpager.setonpagechangelistener (Onpagechangelistener);}  private void Buildactionbarandviewpagertitles () {//Get reference to Action Bar ActionBar ActionBar = Getactionbar ();// Set the mode of the action bar to navigation mode, so that aCtion Bar will display tab tab Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs); Displays only navigation tabs, does not show layout actionbar.setdisplayshowtitleenabled (false) such as the title above Actionbar;                Actionbar.setdisplayuselogoenabled (false); actionbar.setdisplayshowhomeenabled (false); Build a custom adapter that associates Viewpager toggle with the Click of Action Bar tab Maintablistener Maintablistener = new Maintablistener (); Actionbar.addtab (Actionbar.newtab (). SetText (R.string.first). Settablistener (Maintablistener)); ActionBar.addTab ( Actionbar.newtab (). SetText (R.string.second). Settablistener (Maintablistener)); Actionbar.addtab (ActionBar.newTab (). SetText (R.string.thrid). Settablistener (Maintablistener)); Actionbar.addtab (Actionbar.newtab (). SetText ( R.string.fourth). Settablistener (Maintablistener));} Private Fragment getfirstfragment () {return Getfragmentmanager (). Findfragmentbytag (FirstFragment.class.getName ());} Private Fragment getsecondfragment () {return Getfragmentmanager (). Findfragmentbytag (SecondFragment.class.getName () );} prIvate Fragment getthirdfragment () {return Getfragmentmanager (). Findfragmentbytag (ThirdFragment.class.getName ());} Private Fragment getfourthfragment () {return Getfragmentmanager (). Findfragmentbytag (FourthFragment.class.getName ()        );} Build a simple Viewpager toggle listener class that associates the tab click with the viewpager switch viewpager.onpagechangelistener onpagechangelistener = new Viewpager.simpleonpagechangelistener () {@Overridepublic void onpageselected (int position) {Getactionbar (). Setselectednavigationitem (position);}}; Private class Mainuipageradapter extends Appfragmentpageradapter {list<fragment> List = new Arraylist<fragment > ();p ublic mainuipageradapter (fragmentmanager FM) {super (FM); if (getfirstfragment () = = null) {List.add (new Firstfragment ());} else {List.add (getfirstfragment ());} if (getsecondfragment () = = null) {List.add (New secondfragment ());} else {List.add (getsecondfragment ());} if (getthirdfragment () = = null) {List.add (New thirdfragment ());} else {List.add (getthirdfragment ());} if (getfourthfragment() = = null) {List.add (New fourthfragment ());} else {List.add (getfourthfragment ());}} @Overridepublic Fragment getItem (int position) {return list.get (position);} @Overrideprotected String gettag (int position) {list<string> tagslist = new arraylist<string> (); Tagslist.add (FirstFragment.class.getName ()); Tagslist.add (SecondFragment.class.getName ()); Tagslist.add ( ThirdFragment.class.getName ()); Tagslist.add (FourthFragment.class.getName ()); return tagslist.get (position);} @Overridepublic int GetCount () {return list.size ();}} Private class Maintablistener implements Actionbar.tablistener {@Overridepublic void ontabselected (Tab tab, Fragmenttransaction ft) {if (Mviewpager.getcurrentitem ()! = Tab.getposition ()) {Mviewpager.setcurrentitem ( Tab.getposition ());}} @Overridepublic void ontabunselected (Tab tab, fragmenttransaction ft) {//TODO auto-generated method stub}@ overridepublic void ontabreselected (Tab tab, fragmenttransaction ft) {//TODO auto-generated Method stub}}}







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.