I am a beginner in fragment and have written a few simple examples. Now I propose to help you learn and study.
Android fragment Study Notes (1)
The focus of this program is to put two fragment in an activity.
The page layout is shown in the following code (fragment_hide_show.xml ):
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Demonstration of hiding and showing fragments." /> <LinearLayout android:orientation="horizontal" android:padding="4dip" android:gravity="center_vertical" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/frag1hide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hide" /> <fragment android:name="polycom.com.cn.FirstFragment" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0px" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:padding="4dip" android:gravity="center_vertical" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/frag2hide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hide" /> <fragment android:name="polycom.com.cn.SecondFragment" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="0px" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
Each linearlayout has a fragment. The following is the Java code implementation. There are three main types. java file, in which fragmentactivity. java mainly sets two button listeners to control and hide the content in fragment, firstfragment. java, secondfragment. java mainly loads the display page for fragment.
Fragmentactivity. Java
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. widget. button; public class fragmentactivity extends activity {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); SETC Ontentview (R. layout. fragment_hide_show); fragmentmanager fm = getfragmentmanager (); // The following two lines of code add listening events in the activity button to control fragment display and hide addshowhidelistener (R. id. frag1hide, FM. findfragmentbyid (R. id. fragment1); addshowhidelistener (R. id. frag2hide, FM. findfragmentbyid (R. id. fragment2);} void addshowhidelistener (INT buttonid, final fragment) {// get the button final button = (button) Fin IN THE ACTIVITY Dviewbyid (buttonid); button. setonclicklistener (New onclicklistener () {public void onclick (view v) {fragmenttransaction Ft = getfragmentmanager (). begintransaction ();/* sets the fade-in and fade-out effect for fragment. The Android Development Network prompts that these two animation resources are Android internal resources and do not need to be manually defined. */Ft. setcustomanimations (Android. r. animator. fade_in, android. r. animator. fade_out); If (fragment. ishidden () {ft. show (fragment); button. settext ("hidden");} else {ft. hide (fragment); button. settext ("show");} ft. commit ();}});}}
Firstfragment. Java
Import android. app. fragment; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. textview; public class firstfragment extends fragment {textview mtextview; @ override public view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {// load a Layout View v = inf from the file catalog Later. inflate (R. layout. labeled_text_edit, container, true); view TV = v. findviewbyid (R. id. MSG); (textview) TV ). settext ("the fragment saves and restores this text. "); mtextview = (textview) v. findviewbyid (R. id. saved); If (savedinstancestate! = NULL) {mtextview. settext (savedinstancestate. getcharsequence ("text");} return v ;}@ override public void onsaveinstancestate (bundle outstate) {super. onsaveinstancestate (outstate); outstate. putcharsequence ("text", mtextview. gettext ());}}
Secondfragment. Java
Import android. app. fragment; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. textview; public class secondfragment extends fragment {@ override public view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {// loads a Layout View v = Inflater from the file catalog. inflate (R. layout. labeled_text_edit, container, true); view TV = v. findviewbyid (R. id. MSG); (textview) TV ). settext ("The textview saves and restores this text. "); // another textview storage mode (textview) v. findviewbyid (R. id. saved )). setsaveenabled (true); Return v ;}}
The following is the layout file (labeled_text_edit.xml) loaded in fragment)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="4dip" /> <EditText android:id="@+id/saved" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/initial_text" android:freezesText="true"> <requestFocus /> </EditText></LinearLayout>
This is just a simple example. It is only for reference by people who have just learned fragment. I hope that I can play a role here. If you need a complete program, you can download it from my space resources. The Resource Name Is myfragment.