Android native Android ActionBar Tab (swipe) navigation

Source: Internet
Author: User
Tags event listener

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
        Actionbar.tablistener {
    Private Static "Selected_item";
    @Override
     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
        ActionBar
                . AddTab (Actionbar.newtab (). SetText ("first page"). Settablistener (this));
        ActionBar
                . AddTab (Actionbar.newtab (). SetText ("second page"). Settablistener (this));
        ActionBar
                . AddTab (Actionbar.newtab (). SetText ("third page"). Settablistener (this));
    }
    @Override
     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
    @Override
     Public void onsaveinstancestate (Bundle outstate) {
        Outstate.putint (Selected_item, Getactionbar ()
                . Getselectednavigationindex ());
    }
    @Override
     Public void ontabunselected (Actionbar.tab Tab,
            Fragmenttransaction fragmenttransaction) {
    }
    Fires this method when the specified tab is selected
    @Override
     Public void ontabselected (Actionbar.tab Tab,
            Fragmenttransaction fragmenttransaction) {
        To create a new fragment object
        New DummyFragment1 ();
        Creates a bundle object for passing parameters to the fragment
        New Bundle ();
        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);
        Commit a transaction
        Ft.commit ();
    }
    @Override
     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
        Actionbar.tablistener {
    
    Viewpager Viewpager;
    ActionBar ActionBar;
    @Override
     Public void onCreate (Bundle savedinstancestate) {
        Super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_tabswipenav);
        Get Actionbar Object
        ActionBar = Getactionbar ();
        Get Viewpager
        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
            @Override
             Public Fragment getItem (int position) {
                New DummyFragment2 ();
                New Bundle ();
                Args.putint (Dummyfragment2.arg_section_number, Position + 1);
                Fragment.setarguments (args);
                return fragment;
            }
            The return value I of this method indicates how many fragment the adapter total includes
            @Override
             Public int GetCount () {
                return 3;
            }
            The return value of the method determines the title of each fragment
            @Override
             Public Charsequence getpagetitle (int position) {
                Switch (position) {
                 Case 0:
                    return "First page";
                 Case 1:
                    return "second page";
                 Case 2:
                    return "Third page";
                }
                return null;
            }
        };
        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++) {
            ActionBar
                    . AddTab (Actionbar.newtab ()
                            . SetText (Pageradapter.getpagetitle (i))
                            . Settablistener (this));
        }
        Set Fragmentpageradapter for Viewpager components
        To bind an event listener to a Viewpager component
        Viewpager
                . Setonpagechangelistener (new Viewpager.simpleonpagechangelistener () {
                    This method is fired when the fragment of the Viewpager display changes
                    @Override
                     Public void onpageselected (int position) {
                        Actionbar.setselectednavigationitem (position);
                    }
                });
    }
    @Override
     Public void ontabunselected (Actionbar.tab Tab,
            Fragmenttransaction fragmenttransaction) {
    }
    Fires this method when the specified tab is selected
    @Override
     Public void ontabselected (Actionbar.tab Tab,
            Fragmenttransaction fragmenttransaction) {
        Ii
    }
    @Override
     Public void ontabreselected (Actionbar.tab Tab,
            Fragmenttransaction fragmenttransaction) {
    }
}

Download Demo

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.