Because of the Spectator's request, I will write out what the reader wants.
Because the previous article is every fragment instance of the same layout.xml, causing the reader's confusion, this article I let each fragment load a different layout.xml.
First we have to prepare four layout.xml to give each click Jump page.
The first of the Fg_contnet.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= "@color/cornsilk"android:orientation= "Vertical" > <TextView Android:id= "@+id/txt_content"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:gravity= "Center"Android:text= "First"Android:textcolor= "@android: Color/holo_green_light"android:textsize= "20SP"/></linearlayout>
A second fg_contnet2.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= "@color/cornsilk"android:gravity= "Center_vertical"android:orientation= "Vertical" > <TextView Android:id= "@+id/txt_content"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:text= "Second"Android:textcolor= "@android: Color/holo_green_light"android:textsize= "20sp"/> <Button android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Load ...." "/></linearlayout>
A third fg_content3.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= "@color/cornsilk" android:orientation= "vertical" > < ImageView android:layout_width= "Match_parent" android:layout_height= "Wrap_ Content " android:src=" @drawable/ic_launcher " /></linearlayout>
Fourth Fg_content4.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= "@color/cornsilk" android:orientation= "vertical" > < timepicker Android:id= "@+id/timepicker1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
Because you want each fragemnt to load a layout.xml, myfragment also modifies:
We pass a content_layout.xml in the constructor and change the fragmentlayout dynamically.
Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classMyfragmentextendsfragment{Private intcontent_layout; PublicMyfragment (intcontent) { This. Content_layout =content; } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View= Inflater.inflate (Content_layout,container,false); returnview; }}
Finally in the mainactivity:
Importandroid.app.Activity;ImportAndroid.app.FragmentManager;Importandroid.app.FragmentTransaction;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.widget.TextView; Public classMainactivityextendsActivityImplementsview.onclicklistener{//UI Object PrivateTextView Txt_channel; PrivateTextView Txt_message; PrivateTextView Txt_better; PrivateTextView txt_setting; //Fragment Object Privatemyfragment Fg1,fg2,fg3,fg4; PrivateFragmentmanager Fmanager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Fmanager=Getfragmentmanager (); Bindviews (); Txt_channel.performclick (); //simulate a single click, then go in and select the first item } //UI component initialization and event binding Private voidbindviews () {Txt_channel=(TextView) Findviewbyid (R.id.txt_channel); Txt_message=(TextView) Findviewbyid (r.id.txt_message); Txt_better=(TextView) Findviewbyid (r.id.txt_better); Txt_setting=(TextView) Findviewbyid (r.id.txt_setting); Txt_channel.setonclicklistener ( This); Txt_message.setonclicklistener ( This); Txt_better.setonclicklistener ( This); Txt_setting.setonclicklistener ( This); } //Resets the selected state of all text Private voidsetselected () {txt_channel.setselected (false); Txt_message.setselected (false); Txt_better.setselected (false); Txt_setting.setselected (false); } //Hide All Fragment Private voidhideallfragment (fragmenttransaction fragmenttransaction) {if(Fg1! =NULL) Fragmenttransaction.hide (FG1); if(Fg2! =NULL) Fragmenttransaction.hide (FG2); if(Fg3! =NULL) Fragmenttransaction.hide (FG3); if(Fg4! =NULL) Fragmenttransaction.hide (FG4); } @Override Public voidOnClick (View v) {fragmenttransaction ftransaction=fmanager.begintransaction (); Hideallfragment (ftransaction); Switch(V.getid ()) { Caser.id.txt_channel:setselected (); Txt_channel.setselected (true); if(FG1 = =NULL) {FG1=Newmyfragment (r.layout.fg_content); Ftransaction.add (R.ID.LY_CONTENT,FG1); }Else{ftransaction.show (FG1); } Break; Caser.id.txt_message:setselected (); Txt_message.setselected (true); if(Fg2 = =NULL) {Fg2=Newmyfragment (R.layout.fg_content2); Ftransaction.add (R.ID.LY_CONTENT,FG2); }Else{ftransaction.show (FG2); } Break; Caser.id.txt_better:setselected (); Txt_better.setselected (true); if(FG3 = =NULL) {FG3=Newmyfragment (R.LAYOUT.FG_CONTENT3); Ftransaction.add (R.ID.LY_CONTENT,FG3); }Else{ftransaction.show (FG3); } Break; Caser.id.txt_setting:setselected (); Txt_setting.setselected (true); if(Fg4 = =NULL) {Fg4=Newmyfragment (R.LAYOUT.FG_CONTENT4); Ftransaction.add (R.ID.LY_CONTENT,FG4); }Else{ftransaction.show (FG4); } Break; } ftransaction.commit (); }}
Finally look at the effect:
Thank you for your attention ~ Can ' t get is forever, forget was once. ---- can not be forever, forget is once.
Android Fragment (ii) Example 2