Before we learned the action Bar is the standard mode, tab mode as shown.
For tab, we learned in the use of the Android Learning Note (22): Multi-page display-tag, but the Action Bar tab is more suitable for fragment use. We add a navigation tab to the standard action bar that we implemented earlier, with the following code:
public class Tabbedactionbardemo extends SEARCHTESTCASE3implements Actionbar.tablistener{
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Workwithtabbedactionbar ();
}
private void Workwithtabbedactionbar () {
ActionBar bar = Getactionbar ();
Bar.settitle ("Tabbed Action Bar"); You can modify the title of the Action bar set in Androidmanifest.xml
Bar.setnavigationmode (actionbar.navigation_mode_tabs);//default is Actionbar.navigation_mode_standard, which is standard mode. In standard mode, the tab is not rendered.
Actiontab.tab TAB1 =Bar.newtab ();
Tab1.SetText("Tab1");
Tab1.Settablistener(this);If we forget to add Settablistener (), it will be abnormal at run time, and Null will not be set.
Bar.addtab (TAB1);
Bar.addtab (Bar.newtab ()
. SetText ("Tab2")
. Settablistener (this));
}
@Override//actionbar.tablistener Interface, select the callback function again on the selected basis
Public void ontabreselected (Tab tab, fragmenttransaction ft){
Showinfo (Tab.gettext () + "ontabreselected () is called");
}
@Override the//actionbar.tablistener interface, select the callback Function for tab. Currently TAB1, when the user chooses TAB2, first triggers the TAB1 ontabunselected (), then triggers TaB2 's ontableselected ()
Public void ontabselected (Tab tab, fragmenttransaction ft){
Showinfo (Tab.gettext () + "ontabselected () is called");
}
@Override//actionbar.tablistener interface, no longer select (select Other) callback function
Public void ontabunselected (Tab tab, fragmenttransaction ft){
Showinfo (Tab.gettext () + "ontabunselected () is called");
}
}
Actionbar.tab is very suitable for fragment, in different fragment switch, at this time, the specific implementation of Actionbar.tablistener may be fragment inheritance class.
The example code covered in this blog post can be downloaded in Pro Android Learning: Actionbar Small example.
RELATED Links: My Android development related articles
"Turn" Pro Android Learning Note (51): ActionBar (4): Standard and TAB mode