Actionbar tab Tag application and Tablistener method
PackageCom.example.actionBarTest.actionBarTab;Importandroid.app.*;ImportAndroid.os.Bundle;ImportCOM.EXAMPLE.ACTIONBARTEST.R;Importjava.util.ArrayList;Importjava.util.List;/*** Created by Heyiyong on 2014-5-2 morning 9:26.*/ Public classTabactivityextendsActivityImplementsActionbar.tablistener {PrivateList<fragment>fragments; Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.tab_main); //These are ordinary fragment, containing only a textviewHomefragment homefragment =Newhomefragment (); Musicfragment musicfragment=Newmusicfragment (); Videofragment videofragment=Newvideofragment (); Fragments=NewArraylist<fragment>(); Fragments.add (homefragment); Fragments.add (musicfragment); Fragments.add (videofragment); ActionBar ActionBar=Getactionbar (); if(ActionBar! =NULL) { //set Actionbar to tabs modeActionbar.setnavigationmode (Actionbar.navigation_mode_tabs); //Add a few tabs and set the TAP listener eventActionbar.addtab (Actionbar.newtab (). SetText ("Home"). Settablistener ( This)); Actionbar.addtab (Actionbar.newtab (). SetText ("Music"). Settablistener ( This)); Actionbar.addtab (Actionbar.newtab (). SetText ("Video"). Settablistener ( This)); } } /*** This method is called when a tag is selected *@paramtab Click on the selected tab*/@Override Public voidontabselected (Actionbar.tab Tab, fragmenttransaction ft) {Getfragmentmanager (). BeginTransaction (). Replace (R.i D.tab_main, Fragments.get (Tab.getposition ())). commit (); } /*** When you select a different label, the previous label will be unselected, and this method will be called * For example, your current label subscript is 0, now select the label labeled 1, then this method will be called, and the input parameter tab is the original label object * @paramTab The original label object*/@Override Public voidontabunselected (Actionbar.tab Tab, fragmenttransaction ft) {System.out.println ("Ontabunselected Position =" +tab.getposition ()); } /*** This method is called when the current label is selected repeatedly. * For example: The current label is the second one, and when you point the second one with your finger, you call this method *@paramtab The current tab object*/@Override Public voidontabreselected (Actionbar.tab Tab, fragmenttransaction ft) {System.out.println ("Ontabreselected position =" +tab.getposition ()); }}