1. Define framelayout in the layout file of the main page (activity reference fragment), load fragment
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/fl_content"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
</FrameLayout>
2. Initialize Fragment
private void Initfragment () {
Fragment Manager
Fragmentmanager fm = Getsupportfragmentmanager ();
Fragmenttransaction transaction = Fm.begintransaction ();//Start transaction
Replace the frame layout with the corresponding fragment
Transaction
. replace (R.id.fl_content, New Contentfragment (), tag_content);
Transaction.replace (R.id.fl_left_menu, New Leftmenufragment (),
Tag_left_menu);
Transaction.commit ();//Commit a transaction
Fm.findfragmentbytag (tag_content);
}
3. Define fragment base class
Public Abstract classBasefragmentextendsFragment { PublicActivity mactivity; //Fragment was created@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Mactivity= Getactivity ();//Gets the activity object that is located } //initializing the Fragment layout@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V Iew=Initview (); returnview; } //Activity creation End@Override Public voidonactivitycreated (Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate); InitData (); } /*** Initialize layout, subclasses must implement*/ Public AbstractView Initview (); /*** Initialize data, subclass can not implement*/ Public voidInitData () {}}View Code
4. Define fragment base class
Android Project Learning essay Three (Fragment)