Tabactivity for Android

Source: Internet
Author: User
Preface standuptimer, an open-source project on the Android platform during this period, is a simple Android Application designed by jwood to control the conference time, similar to the stopwatch countdown. Tabactivity & tabhost tabactivity inherits from the activity. tabhost is defined internally and can be obtained through gettabhost. Tabhost contains two sub-elements: Some freely selectable tabs and the corresponding content tabcontentto. in layout, they correspond to tabwidget and framelayout respectively. Tabactivity allows the same interface to accommodate more content. We will follow the teamdetailsactivity in standup timer to describe how to use tabactivity. In this example, two tabs are included. One is used to display the statistics of the team and the other is used to display the list of meetings attended by the Team (this is a listview ). When creating layout, you must note that whether you use tabactivity or custom tabhost, tabhost must be used as the root of the XML layout file.

<? XML version = "1.0" encoding = "UTF-8" ?>
< Tabhost Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: ID = "@ Android: ID/tabhost" Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent" >
< Linearlayout Android: Orientation = "Vertical"
Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" >
< Tabwidget Android: ID = "@ Android: ID/tabs"
Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" />
< Framelayout Android: ID = "@ Android: ID/tabcontent"
Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" >

<! --OmittedCode-->

<TextviewAndroid: ID= "@ + ID/no_team_meetings"
Android: textsize= "18sp"Android: layout_width= "Fill_parent"
Android: layout_height= "Fill_parent"/>

< Textview Android: ID = "@ + ID/no_team_meeting_stats"
Android: textsize = "18sp" Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent" />
</ Framelayout >
</ Linearlayout >
</ Tabhost >

 

Generally, we use a linear layout. Therefore, the sub-element of <tabhost> is <linearlayout>. <Tabwidger> corresponding tab. <Framelayout> is used to include the content to be displayed by the tab. Note that the IDs of <tabwidger> and <framelayout> must use the system IDs: Android: ID/tabs and Android: ID/tabcontent. Because the system uses two IDs to initialize two instance variables (mtabwidget and mtabcontent) of tabhost ). To compile Java code, we can use two methods to compile tabs: one is to inherit tabactivity, and then use gettabhost () to obtain the tabhost object; the second method is to use the custom tabhost to customize its ID in the layout file, and then use findviewbyid () to obtain the tabhost object. This document uses the tabactivity inheritance method. Private Void Createtabs (){
Tabhost = Gettabhost ();
Tabhost. addtab (tabhost. newtabspec ( " Stats_tab " ).
Setindicator ( This . Getstring (R. String. Stats )).
Setcontent (createmeetingdetails (Team )));

Tabhost. addtab (tabhost. newtabspec ("Meetings_tab").
Setindicator (This. Getstring (R. String. Meetings )).
Setcontent (createmeetinglist ()));
Gettabhost (). setcurrenttab (0);
}

 

In Java code, the first thing we need to do is to obtain the tabhost object. You can use the gettabhsot () method in tabactivtiy. For custom tabhost, call the setup () method before adding tabs. Mtabhost=(Tabhost) findviewbyid (R. Id. tabhost );
Mtabhost. Setup ();
Mtabhost. addtab (tab_tag_1,"Hello, world!","Tab 1");

 

 
The original text on the SDK:
Call setup () before adding tabs if loading tabhost using findviewbyid ().However :You do not need to call setup () after gettabhost () inTabactivity.


Add tabs to tabhost. That is, call the tabhost. addtab (tabspec) method. Tabspec mainly includes the setindicator and setcontent methods. The two methods are used to specify the tab and tancontent. Tabspec passed. Newtabspec (string tagTo create an instance. Set its attributes after instantiation. Setindicator () sets the tab, which has three overloaded functions

    • Public tabhost. tabspec setindicatior (charswquence label, drawable icon). Specify the tab title and icon.
    • Public tabhost. tabspec (view) uses view to customize a tab
    • Public tabhost. tabspec (charsequence label) specifies the title of the tab, and there is no icon at this time.
Setcontent specifies the display content of the tab, which also has three kinds of overloading
    • Public tabhost. tabspec setcontent (tabhost. tabcontentfactory)
    • Public tabhost. tabspec setcontent (INT viewid)
    • Public tabhost. tabspec setcontent (intent)
After comparison, we can understand that the content displayed is specified by viewid, for example,. setcontent (R. Id. team_edittext ). The third is to directly load a new activity page through intent. For example,. setcontent (new intent (this, meetingactivity. Class); in this example, tabcontentfactory is used to specify the corresponding tabcontent. Tabcontentfactory is an interface that only contains the createtabcontent (string tag) method that returns the view. Private Tabcontentfactory createmeetingdetails (Team Team2 ){

ReturnNewTabhost. tabcontentfactory (){

@ Override
PublicView createtabcontent (string tag ){
// Set view
Setstatstabcontent ();
ReturnFindviewbyid (R. Id. teamstats );
}
};
}

PrivateTabhost. tabcontentfactory createmeetinglist ()
{
ReturnNewTabhost. tabcontentfactory (){

@ Override
PublicView createtabcontent (string tag ){
Meetinglistadapter=Createmeetinglistadapter ();
Meetinglist. setadapter (meetinglistadapter );
ReturnMeetinglist;
}
};
}

 

Declared in advance

Private Listview meetinglist = Null ;
Private Arrayadapter < String > Meetinglistadapter = Null ;

 

We can also use tabactivity to implement the tabcontentfactory interface. Public Class Tabs2 Extends Tabactivity Implements Tabhost. tabcontentfactory

 

Then implement the createtabcontent method in the tabactiviy class.

@ Override
Public View createtabcontent (string tag ){
Final Textview TV = New Textview ( This );
TV. settext ( " Content for tab with Tag " + Tag );
Return TV;
}

 

Setstatstabcontent (); Method

Setstatstabcontent

Private Void Setstatstabcontent (){
If (Team ! = Null && Team. hasmeetings ( This )){
Meetingstats stats = Team. getaveragemeetingstats (teamdetailsactivity. This );
(Textview) findviewbyid (R. Id. meeting_team_name_label). settext (getstring (R. String. team_name ));
(Textview) findviewbyid (R. Id. meeting_team_name). settext (team. getname ());

(Textview) findviewbyid (R. Id. number_of_meetings_label). settext (getstring (R. String. number_of_meetings ));
(Textview) findviewbyid (R. Id. number_of_meetings). settext (integer. tostring ((Int) Team. getnumberofmeetings (teamdetailsactivity.This)));

(Textview) findviewbyid (R. Id. avg_number_of_participant ipants_label). settext (getstring (R. String. avg_number_of_participant ipants ));
(Textview) findviewbyid (R. Id. avg_number_of_participant ipants). settext (float. tostring (stats. getnumparticipant ipants ()));

(Textview) findviewbyid (R. Id. avg_meeting_length_label). settext (getstring (R. String. avg_meeting_length ));
(Textview) findviewbyid (R. Id. avg_meeting_length). settext (timeformathelper. formattime (stats. getmeetinglength ()));

(Textview) findviewbyid (R. Id. avg_individual_status_length_label). settext (getstring (R. String. avg_individual_status_length ));
(Textview) findviewbyid (R. Id. avg_individual_status_length). settext (timeformathelper. formattime (stats. getindividualstatuslength ()));

(Textview) findviewbyid (R. Id. avg_quickest_status_label). settext (getstring (R. String. avg_quickest_status ));
(Textview) findviewbyid (R. Id. avg_quickest_status). settext (timeformathelper. formattime (stats. getquickeststatus ()));

(Textview) findviewbyid (R. Id. avg_longest_status_label). settext (getstring (R. String. avg_longest_status ));
(Textview) findviewbyid (R. Id. avg_longest_status). settext (timeformathelper. formattime (stats. getlongeststatus ()));
}Else{
(Textview) findviewbyid (R. Id. meeting_team_name_label). settext (getstring (R. String. no_meeting_stats ));
(Textview) findviewbyid (R. Id. meeting_team_name). settext ("");

(Textview) findviewbyid (R. Id. number_of_meetings_label). settext ("");
(Textview) findviewbyid (R. Id. number_of_meetings). settext ("");

(Textview) findviewbyid (R. Id. avg_number_of_participant ipants_label). settext ("");
(Textview) findviewbyid (R. Id. avg_number_of_participant ipants). settext ("");

(Textview) findviewbyid (R. Id. avg_meeting_length_label). settext ("");
(Textview) findviewbyid (R. Id. avg_meeting_length). settext ("");

(Textview) findviewbyid (R. Id. avg_individual_status_length_label). settext ("");
(Textview) findviewbyid (R. Id. avg_individual_status_length). settext ("");

(Textview) findviewbyid (R. Id. avg_quickest_status_label). settext ("");
(Textview) findviewbyid (R. Id. avg_quickest_status). settext ("");

(Textview) findviewbyid (R. Id. avg_longest_status_label). settext ("");
(Textview) findviewbyid (R. Id. avg_longest_status). settext ("");
}
}

 

Add tabspec to tabhost, that is, tabhost. addtab (tabspec ). We found that the setindicator and setcontent methods of tabspec return tabspec itself, so we can use the method of code tampering: Tabhost. addtab (tabhost. newtabspec ( " Stats_tab " )
. Setindicator ( This . Getstring (R. String. Stats ))
. Setcontent (createmeetingdetails (Team )));

 

Other references

SDK: tabhost. tabspec

SDK: tabhost. tabcontentfactory

SDK: tabhost

SDK: tab Layout

Tabhost -- tabwidget example

Android tabhost part

Apidemo also has three examples for reference.

Series Indexes
Android open-source project-standuptimer learning notes Index
Related Article

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.