/*fragment Dynamic Loading */
MyFragment2 myfragment2=new MyFragment2 ();/* Create instance */
Fragmentmanager Fragmentmanager = Getfragmentmanager ();/* Get to fragmentmanager*/
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();/* Open transaction */
Fragmenttransaction.add (R.id.frame,myfragment2);
Fragmenttransaction.addtobackstack (null);/* return via physical return key */
Fragmenttransaction.commit ();/* COMMIT TRANSACTION */
Main method
PackageFragmentdemo.example.administrator.fragmentdemo;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.app.FragmentManager;Importandroid.app.FragmentTransaction;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.RadioGroup.OnCheckedChangeListener;/*1) fragment can appear as part of the Activity Interface, (2) Multiple fragment can occur simultaneously in one activity, and one fragment can be used in multiple activity; (3) in the Act Ivity can add, remove, or replace fragment during operation, (4) fragment can respond to their input events and have their own declaration cycles, whose lifecycles are affected by the life cycle of the host activity; (5) fragment in the first painting The Oncreateview () method is called when the user interface is made, this method returns a view. (If the UI is not displayed, NULL is returned); fragment two loading modes: static loading, dynamic loading. */ Public classMainactivityextendsActivityImplementsoncheckedchangelistener{PrivateRadiogroup Group; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Group=(Radiogroup) Findviewbyid (R.id.radiogroup); Group.setoncheckedchangelistener ( This); } @Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) { //TODO auto-generated Method Stub Switch(checkedid) { CaseR.id.first: {Intent Intent=NewIntent ( This, MainActivity2.class); StartActivity (Intent); Break; } CaseR.id.second: {/*Fragment Dynamic Loading*/MyFragment2 MyFragment2=NewMyFragment2 ();/*Create an instance*/Fragmentmanager Fragmentmanager= Getfragmentmanager ();/*get to Fragmentmanager*/fragmenttransaction fragmenttransaction= Fragmentmanager.begintransaction ();/*Open Transaction*/Fragmenttransaction.add (R.id.frame,myfragment2); Fragmenttransaction.addtobackstack (NULL);/*returned by a physical return key*/fragmenttransaction.commit ();/*Commit a transaction*/ Break; } CaseR.id.thrid: {Intent Intent=NewIntent ( This, MainActivity3.class); StartActivity (Intent); Break; } CaseR.id.fourth: {Intent Intent=NewIntent ( This, MainActivity4.class); StartActivity (Intent); Break; } } }}
Load the main.xml layout, specifying the ID of the Linerlayout
1<?xml version= "1.0" encoding= "Utf-8"?>2<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent" >5 6<LinearLayout7Android:id= "@+id/frame"8Android:layout_width= "Match_parent"9android:layout_height= "Wrap_content"Tenandroid:orientation= "Vertical" > One</LinearLayout> A - - the<Radiogroup -Android:id= "@+id/radiogroup" -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" +Android:layout_alignparentbottom= "true" -Android:gravity= "Center_horizontal" +android:orientation= "Horizontal" > A at<RadioButton -Android:id= "@+id/first" -Android:layout_width= "0DP" -android:layout_height= "Wrap_content" -android:layout_weight= "1" -android:background= "@drawable/radio_pressed" inandroid:button= "@null" -android:drawabletop= "@mipmap/ic_launcher" toAndroid:gravity= "Center_horizontal" +android:text= "Static Load"/> - the<RadioButton *Android:id= "@+id/second" $Android:layout_width= "0DP"Panax Notoginsengandroid:layout_height= "Wrap_content" -android:layout_weight= "1" theandroid:background= "@drawable/radio_pressed" +android:button= "@null" Aandroid:drawabletop= "@mipmap/ic_launcher" theAndroid:gravity= "Center_horizontal" +android:text= "Dynamic load"/> - $<RadioButton $Android:id= "@+id/thrid" -Android:layout_width= "0DP" -android:layout_height= "Wrap_content" theandroid:layout_weight= "1" -android:background= "@drawable/radio_pressed"Wuyiandroid:button= "@null" theandroid:drawabletop= "@mipmap/ic_launcher" -Android:gravity= "Center_horizontal" WuAndroid:text= "Life cycle"/> - About<RadioButton $Android:id= "@+id/fourth" -Android:layout_width= "0DP" -android:layout_height= "Wrap_content" -android:layout_weight= "1" Aandroid:background= "@drawable/radio_pressed" +android:button= "@null" theandroid:drawabletop= "@mipmap/ic_launcher" -Android:gravity= "Center_horizontal" $android:text= "Transmit value communication"/> the</RadioGroup> the the</RelativeLayout>
MyFragment2 inherited from Fragment
1 PackageFragmentdemo.example.administrator.fragmentdemo;2 3 Importandroid.app.Fragment;4 ImportAndroid.os.Bundle;5 Importandroid.support.annotation.Nullable;6 ImportAndroid.view.LayoutInflater;7 ImportAndroid.view.View;8 ImportAndroid.view.ViewGroup;9 ImportAndroid.widget.TextView;Ten One /** A * Created by Administrator on 2016/5/6. - */ - Public classMyFragment2extendsfragment{ the @Nullable - @Override - PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { - /*resource:fragment The layout file that needs to be loaded + Root: Loading the parent viewgroup in Layout - Attachtoroot:false, do not return parent ViewGroup*/ +View View=inflater.inflate (R.layout.fragment,container,false); ATextView textview=(TextView) View.findviewbyid (r.id.text); atTextview.settext ("Dynamic Load"); - returnview; - - } -}
Fragment.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 " ><TextView android:layout_width= "Wrap_content" android:layout_height= "Wrap_ Content " android:id=" @+id/text " /> <button android:text=" Change " android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:id= "@+id/button"/></linearlayout>
Fragment Dynamic loading