By dynamically loading the fragment implementation in an activity with 3 different layouts, look directly at it:
In the general mode:
Click Home Control:
Code :
androidmanifest.xml--did not make any changes to create the project by default
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android " package=" com.wxl.fragment " android:versioncode=" 1 " android:versionname=" 1.0 "> < USES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <application android: Allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android: Theme= "@style/apptheme" > <activity android:name= "com.wxl.fragment.MainActivity" android: Label= "@string/app_name" > <intent-filter> <action android:name= " Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application></manifest>
Fragment1.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 " android: Background= "#ffcccc" android:orientation= "vertical" > <textview android:id= "@+id/fragment1_ TextView " android:layout_width=" match_parent " android:layout_height=" Match_parent " android: gravity= "center" android:text= "First Fragment Interface" android:textsize= "50SP"/></linearlayout>
Fragment2.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 " android: Background= "#00ffff" android:orientation= "vertical" > <textview android:id= "@+id/fragment2_ TextView " android:layout_width=" match_parent " android:layout_height=" match_parent " android:gravity = "Center" android:text= "second fragment Interface" android:textsize= "50sp"/> </LinearLayout>
Fragment3.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 " android: Background= "#ffff00" android:orientation= "vertical" > <textview android:id= "@+id/fragment2_ TextView " android:layout_width=" match_parent " android:layout_height=" match_parent " android:gravity = "Center" android:text= "third fragment Interface" android:textsize= "50sp"/> </LinearLayout>
Activity_main.xml
<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:orien tation= "vertical" android:background= "#000000" android:id= "@+id/main" > <relativelayout android: Layout_width= "Match_parent" android:layout_height= "80DP" android:background= "@drawable/main_menu_bk" android:orientation= "Vertical" > <!--android:splitmotionevents= "false" if multiple view is streaming touch events--<l Inearlayout android:layout_width= "wrap_content" android:layout_height= "Match_parent" Android:orienta tion= "Horizontal" android:layout_centerhorizontal= "true" android:splitmotionevents= "false" > <l Inearlayout android:id= "@+id/menu_1" android:layout_width= "250DP" android:layout_height= "Match_parent" android:orientation= "Vertical "> <textview android:id=" @+id/textview1 "android:layout_width=" Wra P_content "android:layout_height=" match_parent "android:layout_gravity=" center " android:gravity= "center" android:text= "normal Mode" android:textcolor= "#ffffffff" Android:textsize= "28sp"/> </LinearLayout> <view android:layout_width= "4DP" android:layout_height= "Match_parent" android:layout_marginbottom= "10DP" Android:layout_margi ntop= "10DP" android:background= "Android:attr/listdivider"/> <linearlayout android:id= "@+id/menu_2" android:layout_width= "250DP" android:layout_height= "Match_parent" Android:o rientation= "vertical" > <textview android:id= "@+id/textview2" android:layou T_width= "Wrap_content" android:layout_height= "match_parent" android:layout_gravity= "center" android:g ravity= "center" android:text= "Home Control" android:textcolor= "#ffffffff" Android:tex Tsize= "28sp"/> </LinearLayout> <view android:layout_width= "3DP" Android oid:layout_height= "Match_parent" android:layout_marginbottom= "10DP" android:layout_margintop= "10DP" Android:background= "? Android:attr/listdivider"/> <linearlayout android:id= "@+id/menu_3 "Android:layout_width=" 250DP "android:layout_height=" Match_parent "android:orientation=" Vertical "> <textview android:id=" @+id/textview3 "android:layout_width=" Wra P_content "android:layout_height=" match_parent "android:layout_gravity=" center " android:gravity= "centEr "android:text=" Happy Community "android:textcolor=" #ffffffff "android:textsize=" 28SP " /> </LinearLayout> </LinearLayout> </RelativeLayout> <linearlayout android: Id= "@+id/root" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:orien tation= "Vertical" > </LinearLayout></LinearLayout>
Fragment1.java
Package Com.wxl.fragment;import Android.annotation.suppresslint;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import android.view.ViewGroup;@ Suppresslint ("Newapi") public class Fragment1 extends Fragment {@Overridepublic View oncreateview (layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {//TODO auto-generated method Stubreturn inflater.inflate ( R.layout.fragment1, Container,false);}}
Fragment2.java
Package Com.wxl.fragment;import Android.annotation.suppresslint;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import android.view.ViewGroup;@ Suppresslint ("Newapi") public class Fragment2 extends Fragment {@Overridepublic View oncreateview (layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {//TODO auto-generated method Stubreturn inflater.inflate ( R.layout.fragment2, Container,false);}}
Fragment3.java
Package Com.wxl.fragment;import Android.annotation.suppresslint;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import android.view.ViewGroup;@ Suppresslint ("Newapi") public class Fragment3 extends Fragment {@Overridepublic View oncreateview (layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {//TODO auto-generated method Stubreturn inflater.inflate ( R.layout.fragment3, Container,false);}}
Mainactivity.java
Package Com.wxl.fragment;import Android.annotation.suppresslint;import Android.app.activity;import Android.app.fragment;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.Window; Import Android.widget.linearlayout;public class Mainactivity extends Activity implements Onclicklistener{public static Final int normal_mode = 0;public static final int smarthome_mode = 1;public static final int community_mode = 2; Fragmentmanager fragmentmanager;private int mmode = normal_mode;private Fragment mfragment;//record which fragmentprivate is currently in Fragment1 fragment1;private Fragment2 fragment2;private Fragment3 fragment3; @SuppressLint ("Newapi") @Override protected void OnCreate (Bundle savedinstancestate) {linearlayout menu1, menu2, MENU3; Super.oncreate (savedinstancestate); This.requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activitY_main); menu1 = (linearlayout) This.findviewbyid (r.id.menu_1); Menu1.setonclicklistener (this); MENU2 = (linearlayout) This.findviewbyid (r.id.menu_2); Menu2.setonclicklistener (this); MENU3 = (linearlayout) This.findviewbyid (r.id.menu_3); Menu3.setonclicklistener (this); Fragmentmanager = Getfragmentmanager (); /*fragmentmanager.begintransaction () Gets the Fragmenttransaction object * Hide (mfragment) hides the current fragment * */FRA Gment1 = new Fragment1 (); if (fragment1.isadded ()) {fragmentmanager.begintransaction (). Show (Fragment1). Hide (Mfragment). commit ();} else {Frag Mentmanager.begintransaction (). Add (R.id.root, Fragment1). Show (Fragment1). commit ();} Mfragment = Fragment1;} @SuppressLint ("Newapi") @Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubint mode = Normal_mode; Switch (Arg0.getid ()) {case r.id.menu_1:{mode = Normal_mode;} Break;case R.id.menu_2:{mode = SmarthomE_mode;} Break;case r.id.menu_3:{mode = Community_mode;} break;} if (Mmode = = mode) Return;mmode = mode; Fragmenttransaction fragmenttransaction;fragmenttransaction = Fragmentmanager.begintransaction (); if (mode = = NORMAL_ MODE) {if (fragment1 = = null) {fragment1 = new Fragment1 ();} if (fragment1.isadded ()) {fragmenttransaction.show (fragment1). Hide (Mfragment). commit ();} else { Fragmenttransaction.add (R.id.root, Fragment1). Show (Fragment1). Hide (Mfragment). commit ();} Mfragment = Fragment1;} else if (mode = = Smarthome_mode) {if (Fragment2 = = null) {Fragment2 = new Fragment2 ();} if (fragment2.isadded ()) {fragmenttransaction.show (Fragment2). Hide (Mfragment). commit ();} else { Fragmenttransaction.add (R.id.root, Fragment2). Show (Fragment2). Hide (Mfragment). commit ();} Mfragment = Fragment2;} else if (mode = = Community_mode) {if (Fragment3 = = null) {Fragment3 = new Fragment3 ();} if (fragment3.isadded ()) {fragmenttransaction.show (FRAGMENT3). Hide (Mfragment). commit ();} else { Fragmenttransaction.add (R.id.root, FragmeNT3). Show (Fragment3). Hide (Mfragment). commit ();} Mfragment = Fragment3;}}}Note:
The same Fragmenttransaction object can only have one commit () at a time, otherwise it prompts "Java.lang.IllegalStateException:commit already called".
Source Download :
http://download.csdn.net/detail/thanksgining/8406745
Android UI Programming (8)--Dynamic loading fragment