Content of this article
- Environment
- Project structure
- Demo One: ActionBar Tab navigation
- Demo two: ActionBar Tab with sliding navigation
This article demonstrates Tab navigation. The first presentation is the Basic tab navigation, and the second is tab navigation with a swipe.
In addition, personally feel, through this example can know, how to create the initialization of Fragment, and put Fragment into the "container". A container can be either linearlayout, Relativelayout, or ViewGroup. This is similar to the implementation of the initialization Web application page, bothered me for a long time, do not solve the problem, unable to write their own Android app.
Fragment fragments, this idea is very good. It's a new Android 3.0, and with Fragment, you can modularize your Activity. It's like, now the page is using div (layer) concept, I think, this probably borrowed from the Photoshop layer (layers) concept.
Download Demo Environment
- Windows R2 64-bit
- Eclipse ADT v22.6.2,android 4.4.2 (API 19)
- SAMSUNG gt-8618,android OS 4.1.2
Project structure
Figure 1 Project Structure Figure 2 Main program Interface
1, the only thing to note is thatDummiyFragment1 and DummiyFragment2 are identical, but they inherit different classes. DummiyFragment1 Inherits Android.app.Fragment, while DummiyFragment2 inherits Android.support.v4.Fragment.
Demo ActionBar Tab Navigation
Figure 3 Tab Navigation
The core code is as follows:
Public class Actionbartabnavtest extends Activity implements
Private Static "Selected_item";
Public void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_tabnav);
Final ActionBar ActionBar = Getactionbar ();
To set Actionbar Navigation: Tab navigation
Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs);
Add 3 tab pages, and add an event listener for 3 tab labels
. AddTab (Actionbar.newtab (). SetText ("first page"). Settablistener (this));
. AddTab (Actionbar.newtab (). SetText ("second page"). Settablistener (this));
. AddTab (Actionbar.newtab (). SetText ("third page"). Settablistener (this));
Public void onrestoreinstancestate (Bundle savedinstancestate) {
if (Savedinstancestate.containskey (Selected_item)) {
Select the fragment page for the previously saved index
Getactionbar (). Setselectednavigationitem (
Savedinstancestate.getint (Selected_item));
Saves the index of the currently selected fragment page to the bundle
Public void onsaveinstancestate (Bundle outstate) {
Outstate.putint (Selected_item, Getactionbar ()
. Getselectednavigationindex ());
Public void ontabunselected (Actionbar.tab Tab,
Fragmenttransaction fragmenttransaction) {
Fires this method when the specified tab is selected
Public void ontabselected (Actionbar.tab Tab,
Fragmenttransaction fragmenttransaction) {
To create a new fragment object
Creates a bundle object for passing parameters to the fragment
Args.putint (Dummyfragment2.arg_section_number, tab.getposition () + 1);
Passing parameters to fragment
Fragment.setarguments (args);
Get Fragmenttransaction Object
Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
Use fragment instead of the container component in the activity
Ft.replace (R.id.container, fragment);
Public void ontabreselected (Actionbar.tab Tab,
Fragmenttransaction fragmenttransaction) {
Demo ActionBar Tab with sliding navigation
Figure 4 Tab Slide navigation
The core code is as follows:
Public class Actionbartabswipenavtest extends Fragmentactivity implements
Public void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_tabswipenav);
ActionBar = Getactionbar ();
Viewpager = (Viewpager) Findviewbyid (R.id.pager);
Creates a Fragmentpageradapter object that is responsible for providing multiple fragment for Viewpager
New Fragmentpageradapter (
Getsupportfragmentmanager ()) {
Get fragment of location position
Public Fragment getItem (int position) {
Args.putint (Dummyfragment2.arg_section_number, Position + 1);
Fragment.setarguments (args);
The return value I of this method indicates how many fragment the adapter total includes
The return value of the method determines the title of each fragment
Public Charsequence getpagetitle (int position) {
Setting Actionbar using the tab navigation method
Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs);
Iterates through all the fragment contained by the Pageradapter object.
Create a tab label for each fragment
for (int i = 0; i < Pageradapter.getcount (); i++) {
. AddTab (Actionbar.newtab ()
. SetText (Pageradapter.getpagetitle (i))
. Settablistener (this));
Set Fragmentpageradapter for Viewpager components
To bind an event listener to a Viewpager component
. Setonpagechangelistener (new Viewpager.simpleonpagechangelistener () {
This method is fired when the fragment of the Viewpager display changes
Public void onpageselected (int position) {
Actionbar.setselectednavigationitem (position);
Public void ontabunselected (Actionbar.tab Tab,
Fragmenttransaction fragmenttransaction) {
Fires this method when the specified tab is selected
Public void ontabselected (Actionbar.tab Tab,
Fragmenttransaction fragmenttransaction) {
Public void ontabreselected (Actionbar.tab Tab,
Fragmenttransaction fragmenttransaction) {
Download Demo