Android UI Tab (Actionbar+fragment implementation)

Source: Internet
Author: User

Fragment and Actionbar are Android3.0 after the advent of Fragment, fragments, mainly to support more UI design on large screen devices such as tablets. Now that the device's screen is getting bigger and larger, using fragment can manage changes in the view hierarchy more flexibly. Like activity, you can create fragment to contain the view, layout, but fragment must be embedded in the activity, not alone, and an activity can embed multiple fragment, At the same time a fragment can be reused by multiple activity. Action Bar is considered to be the most important interaction element in the new version of Android, and is always at the top of the program, and it is mainly played by highlighting important actions, maintaining consistent page navigation and switching in the program, and placing low-frequency functions on the action Overflow, save page space, a fixed area display program indicator.

Actionbar+fragment steps to implement the Tab tab:
1, define each fragment, and activity layout. The content portion of the tab fragment the position in the activity layout accordingly, android:id= "@+id/fragment_content".
2, get Actionbar in activity, set tab mode.
3. New Tab tab.
4, Declare tab (Fragment), and add listeners. Let tab tab (ACTIONBAR.TAB), content (Fragment), listen for event Association, ontabselected ontabunselected tab Content Fragment switch in listener event.
5. Add tab to Actionbar.

Example: Actionbartabandfragmentdemo
Operating effect:


Code Listing:
Layout file: Action_bar_tabs.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" Match_parent "a ndroid:orientation= "vertical" > <framelayout android:id= "@+id/fragment_content" Android:layout_wid Th= "Match_parent" android:layout_height= "0dip" android:layout_weight= "1"/> <line Arlayout android:layout_width= "match_parent" android:layout_height= "0dip" android:layout                _weight= "1" android:orientation= "vertical" > <button android:id= "@+id/btn_add_tab" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "                @string/btn_add_tab "android:onclick=" Onaddtab "/> <button android:id=" @+id/btn_remove_tab " Android:layout_width= "Wrap_content "android:layout_height=" wrap_content "android:text=" @string/btn_remove_tab " android:onclick= "Onremovetab"/> <button android:id= "@+id/btn_toggle_tabs" Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/btn_                Toggle_tabs "android:onclick=" Ontoggletabs "/> <button android:id=" @+id/btn_remove_all_tabs " Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Andro id:text= "@string/btn_remove_all_tabs" android:onclick= "Onremovealltabs"/> </linearlayout></ Linearlayout>

Layout file: Action_bar_tab_content.xml
<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android "          android:id=" @+id/text "          android:layout_width=" wrap_content "          android:layout_height=" Wrap_ Content "/>

Java source code file: Mainactivity.java
Package Com.rainsong.actionbartabandfragmentdemo;import Android.app.actionbar;import Android.app.ActionBar.Tab; Import Android.app.activity;import Android.app.fragment;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup; Import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Activity {@Override PR        otected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.action_bar_tabs);        public void Onaddtab (View v) {final ActionBar bar = Getactionbar ();        Final int tabcount = Bar.gettabcount ();        Final String Text = "tab" + Tabcount; Bar.addtab (Bar.newtab (). SetText (text). Settablistener (New Tablistener    T (text))));        public void Onremovetab (View v) {final ActionBar bar = Getactionbar (); If (Bar.gettabcount () > 0)        {Bar.removetabat (Bar.gettabcount ()-1);        }} public void Ontoggletabs (View v) {final ActionBar bar = Getactionbar (); if (bar.getnavigationmode () = = Actionbar.navigation_mode_tabs) {Bar.setnavigationmode (actionbar.navigation_mod            E_standard);        Bar.setdisplayoptions (Actionbar.display_show_title, actionbar.display_show_title);            } else {Bar.setnavigationmode (actionbar.navigation_mode_tabs);        Bar.setdisplayoptions (0, Actionbar.display_show_title);    }} public void Onremovealltabs (View v) {Getactionbar (). Removealltabs ();        } Private class Tablistener implements Actionbar.tablistener {private tabcontentfragment mfragment;        Public Tablistener (Tabcontentfragment fragment) {mfragment = fragment; } public void ontabselected (Tab tab, fragmenttransaction ft) {ft.add (r.id.fragment_content, Mfragment, Mfragment.GetText ());        } public void ontabunselected (Tab tab, fragmenttransaction ft) {ft.remove (mfragment); } public void ontabreselected (Tab tab, fragmenttransaction ft) {toast.maketext (Mainactivity.this, "Rese        lected! ", Toast.length_short). Show ();        }} Private class Tabcontentfragment extends Fragment {private String mText;        Public tabcontentfragment (String text) {mText = text;        } public String GetText () {return mText; } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle sav            Edinstancestate) {View Fragview = inflater.inflate (R.layout.action_bar_tab_content, container, false);            TextView Text = (TextView) Fragview.findviewbyid (R.id.text);            Text.settext (MText);        return fragview; }    }}


API Knowledge Points
public class
Activity
Extends Contextthemewrapper
Implements COMPONENTCALLBACKS2 Keyevent.callback Layoutinflater.factory2 View.oncreatecontextmenulistener Window.callback

Known Direct subclasses
Accountauthenticatoractivity, Activitygroup, aliasactivity, expandablelistactivity, FragmentActivity, ListActivity, Nativeactivity

Known Indirect subclasses
Actionbaractivity, Launcheractivity, preferenceactivity, tabactivity

Class Overview
An activity was a single, focused thing that the user can do. Almost all activities interact with the user, and the Activity class takes care of creating a window for you in which C A place your UI with Setcontentview (View).

ActionBar Getactionbar ()
Retrieve a reference to this activity ' s ActionBar.


Public abstract class
ActionBar
Extends Object

Class Overview
A window feature at the top of the activity so may display the activity title, navigation modes, and other interactive I TEMs.

abstract int Getnavigationmode ()
Returns the current navigation mode.

abstract void Setnavigationmode (int mode)
Set the current navigation mode.

abstract void setdisplayoptions (int options, int mask)
Set selected display options.

abstract int Gettabcount ()
Returns the number of tabs currently registered with the Action Bar.

Abstract Actionbar.tab NewTab ()
Create and return a new actionbar.tab.

abstract void AddTab (Actionbar.tab Tab)
ADD a tab for use in tabbed navigation mode.

abstract void Removetabat (int position)
Remove a tab from the Action bar.

abstract void Removealltabs ()
Remove all tabs from the Action Bar and deselect the current tab.


public static abstract class
Actionbar.tab
Extends Object

Class Overview
A tab in the Action bar.
Tabs manage the hiding and showing of fragments.

Abstract Actionbar.tab SetText (charsequence text)
Set the text displayed on this tab.

Abstract Actionbar.tab Settablistener (Actionbar.tablistener Listener)
Set the Actionbar.tablistener that would handle switching to and from this tab.

public static interface
Actionbar.tablistener

Class Overview
Callback interface Invoked when a tab is focused, unfocused, added, or removed.

abstract void ontabreselected (Actionbar.tab Tab, fragmenttransaction ft)
Called when a tab is already selected was chosen again by the user.

abstract void ontabselected (Actionbar.tab Tab, fragmenttransaction ft)
Called when a tab enters the selected state.

abstract void ontabunselected (Actionbar.tab Tab, fragmenttransaction ft)
Called when a tab exits the selected state.


Public abstract class
fragmenttransaction
Extends Object

Class Overview
API for performing a set of Fragment operations.

Abstract fragmenttransaction Add (int containerviewid, Fragment Fragment, String tag)
Add a fragment to the activity state.

Abstract fragmenttransaction Remove (Fragment Fragment)
Remove an existing fragment.

public class
Fragment
Extends Object
Implements COMPONENTCALLBACKS2 View.oncreatecontextmenulistener

Class Overview
A Fragment is a piece of a application's user interface or behavior that can being placed in an Activity.

View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate)
Called to has the fragment instantiate its user interface view.

Android UI Tab (Actionbar+fragment implementation)

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.