Fragment Loading method
There are two ways to load a method, either by registering it in an XML file or by loading it in Java code.
Registering in XML
For example, defined in Fragment_demo.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7 <Fragment8 Android:id= "@+id/main_fragment_up"9 Android:name= "Com.rust.fragment.FirstFragment"Ten Android:layout_width= "Match_parent" One Android:layout_height= "0DP" A Android:layout_weight= "1" /> - - <Fragment the Android:id= "@+id/main_fragment_bottom" - Android:name= "Com.rust.fragment.SecondFragment" - Android:layout_width= "Match_parent" - Android:layout_height= "0DP" + Android:layout_weight= "1" /> - + </LinearLayout>
Com.rust.fragment.SecondFragment is fragment sub-class
In Secondfragment.java , the Oncreateview method is replicated and the defined view is returned.
Load directly in the activity
Setcontentview (R.layout.fragment_demo);
Loading in Java code
① prepare the fragment XML layout file.
② Create a new class, inherit from fragment; Find the fragment layout file in this class
③ uses Fragmentmanager to manipulate fragment in activity
④, don't forget the commit.
Customize a layout file first fragment_first.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayout3 xmlns:android= "Http://schemas.android.com/apk/res/android"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "vertical"7 Android:background= "#0011ff" >8 9 <!--<buttonTen android:id= "@+id/btn_fragment1_1" One android:layout_width= "Wrap_content" A android:layout_height= "Wrap_content" - android:text= "@string/btn_fragment1" - android:textsize= "16SP" the /> - <edittext - /> - - + </LinearLayout>
Creates a new class Firstfragment.java , inherited from fragment. Replication Oncreateview method. In the Oncreateview method, you can manipulate the controls on the fragment.
1 @Override2 PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {3View Rootview = inflater.inflate (R.layout.fragment_first, container,false);4 5 //Fragment_first is a well-defined layout6 7 //If a control is placed on this fragment, such as Button,edittext. You can define the action here .8 9Btn_fragment1_send =(Button) Rootview.findviewbyid (r.id.btn_fragment1_1);Ten One //... A returnRootview; -}
Prepare a position for the fragment, such as using Framelayout to occupy a place in the activity_main.xml .
1 <Framelayout2 3 Android:id= "@+id/layout_container1"4 Android:layout_width= "Match_parent"5 Android:layout_height= "0DP"6 Android:layout_weight= "4" >7 8 </Framelayout>
In Mainactivity.java , get Fragmentmanager first, get fragmenttransaction. The addition and deletion of fragment is done by Fragmenttransaction.
F1 =NewFirstfragment ();//Get InstanceF2=NewSecondfragment ();//fragmenttransaction fragmenttransaction=Getfragmentmanager (). BeginTransaction (); Fragmenttransaction.add (R.ID.LAYOUT_CONTAINER1,F1); //AddFragmenttransaction.replace (R.ID.LAYOUT_CONTAINER1,F1); //Replace//or it can be writtenfragmenttransaction.replace (R.id.layout_container1,Newfirstfragment ());//fragmenttransaction.addtobackstack (NULL); //added to the return stack, so you can return the added fragment when you press the return keyFragmenttransaction.commit (); //don't forget the commit.//Remove Operation Getfragmentmanager (). BeginTransaction (). Remove (F1). commit ();
Code loading is more flexible than registering with XML. Individuals prefer dynamic loading.
Android-fragment (ii) loading Fragment