Android Actionbar combined with fragment Demo2

Source: Internet
Author: User

The previous article introduced the use of Actionbar, here actionbar another way to use, to achieve the effect and the previous groupactivity or Tabhost is the same, can be used as a navigation.

Realize:


Source:


Layout file: Activity_main:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/main "    android:layout_width=" Match_parent "    android: layout_height= "Match_parent"    tools:context= ". Mainactivity "     android:background=" @android: Color/white "></RelativeLayout>

F1.xml (Sports news corresponding layout file):

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <textview        android:id=" @+id/textview1 "        android:layout_width=" Wrap_content "        android:layout_height= "wrap_content"        android:layout_gravity= "center_horizontal"        android:text= " Welcome to Sports News ... "         android:textsize=" 20sp "        android:textcolor=" @android: Color/holo_blue_dark "/>    <ratingbar        android:id= "@+id/ratingbar1"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"/></linearlayout>

F2.xml (Entertainment News):

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= ". Mainactivity ">    <textview        android:id=" @+id/textview1 "        android:layout_width=" Wrap_content "        android:layout_height= "wrap_content"        android:layout_gravity= "center_horizontal"        android:text= " Welcome to the Entertainment news ... "        android:textcolor=" @android: Color/holo_green_dark "        android:textsize=" 20SP "/></ Linearlayout>

F3.xml (Military News):

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= ". Mainactivity ">    <textview        android:id=" @+id/textview1 "        android:layout_width=" Wrap_content "        android:layout_height= "wrap_content"        android:layout_gravity= "center_horizontal"        android:text= " Welcome to Military News ... "        android:textcolor=" @android: Color/holo_orange_dark "        android:textsize=" 20SP "/></ Linearlayout>

Code files:

Mainactivity:

Package Com.fragmentdemo10_actionbar;import Android.app.actionbar;import Android.app.actionbar.tab;import Android.app.actionbar.tablistener;import Android.app.activity;import Android.app.fragmenttransaction;import Android.os.bundle;public class Mainactivity extends Activity {private ActionBar actionbar;/** * Set three integer constants, respectively 0,1,2; corresponding : Sports News, entertainment news, military news. */private final int SPORTS = 0;private final int entertainment = 1;private final int MILITARY = 2; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); ActionBar = Getactionbar ();//Set ActionBar navigation mode Actionbar.setnavigationmode (actionbar.navigation_mode_tabs); Actionbar.setdisplayoptions (0, actionbar.display_show_title);/** * Add three tabs, respectively: Sports News, entertainment news, military news. */actionbar.addtab (Actionbar.newtab (). SetText ("Sports News"). SetIcon (R.drawable.ic_launcher). Settablistener (New Mytablistener ()). Settag (SPORTS)), Actionbar.addtab (Actionbar.newtab (). SetText ("Entertainment News"). SetIcon (R.drawable.ic_ LaunCher). Settablistener (New Mytablistener ()). Settag (Entertainment)); Actionbar.addtab (Actionbar.newtab (). SetText (" Military News "). SetIcon (R.drawable.ic_launcher). Settablistener (New Mytablistener ()). Settag (MILITARY));} Class Mytablistener implements Tablistener {@Overridepublic void ontabselected (Tab tab, fragmenttransaction ft) {switch ( Integer.parseint (Tab.gettag (). toString ())) {/** * corresponds to sports news */case SPORTS:ft.replace (R.id.main, New Fragementa ()); break /** * corresponds to entertainment news */case ENTERTAINMENT:ft.replace (R.id.main, New Fragementb ()); break;/** * Corresponding military news */case MILITARY:ft.replace (R.id.main, New Fragementc ()); break;default:break;}} @Overridepublic void ontabunselected (Tab tab, fragmenttransaction ft) {} @Overridepublic void ontabreselected (Tab tab, Fragmenttransaction ft) {}}}
Fragmenta (Tab sports News corresponding to fragment):
Package Com.fragmentdemo10_actionbar;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import android.view.view;import android.view.viewgroup;/** * Tab Sports News corresponding Fragment * */ public class Fragementa extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container , Bundle savedinstancestate) {View view = Inflater.inflate (R.LAYOUT.F1, null); return View;}}
FRAGMENTB (Tab Entertainment news corresponding to fragment):
Package Com.fragmentdemo10_actionbar;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import android.view.view;import android.view.viewgroup;/** * Tab Entertainment NEWS corresponding Fragment * */ public class Fragementb extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container , Bundle savedinstancestate) {View view = Inflater.inflate (R.LAYOUT.F2, null); return View;}}
FRAGMENTC (Tab military news corresponding to fragment):
Package Com.fragmentdemo10_actionbar;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import android.view.view;import android.view.viewgroup;/** * Tab Military News corresponding fragment * */ public class Fragementc extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container , Bundle savedinstancestate) {View view = Inflater.inflate (r.layout.f3, null); return View;}}

Source code Download:

Click to download the source code

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.