Android ActionBar about tab application and how to Tablistener

Source: Internet
Author: User

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 ()); }}

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.