"Turn" Pro Android Learning Note (51): ActionBar (4): Standard and TAB mode

Source: Internet
Author: User

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

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.