Preface: This article is the use of fragment the most basic knowledge, only for beginners to watch, but also for my future to do the basic preparation of relevant knowledge. If it's a big God, it can be a waste of a few minutes.
For the basics of Fragment those APIs, you can click here to view Android Fragment Basic Introduction This article will not be described.
First, let's look at the interface
The TextView on the left will vary depending on the button on the right.
Here's how the code begins:
1. Create a new fragment1.xml in layout
<?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= "#00ff00" android:orientation= "vertical" > <textview android:id= "@+id/fragment_ Text " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:text=" this is fragment 1 " android:textcolor=" #000000 " android:textsize=" 25SP "/></linearlayout>
As you can see, there's only one textview here.
2. Create a new fragment2.xml in layout
<?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=" #ffff00 " android:orientation= "vertical" > <textview android:layout_width= "wrap_content" Android:layout_heig ht= "Wrap_content" android:text= "This is Fragment 2" android:textcolor= "#000000" android:textsize= "25s P "/> <button android:id=" @+id/button "android:layout_width=" Wrap_content "android:layout_he ight= "wrap_content" android:text= "num 1"/> <button android:id= "@+id/button2" android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:text= "num 2"/> <button A Ndroid:id= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and roid:text= "Num 3"/></linearlAyout>
Here are three button
3. Create Class Fragment1 inheritance Fragment
Package Lgx.fram.framents;import Android.app.fragment;import Android.os.bundle;import android.view.LayoutInflater; Import Android.view.view;import Android.view.viewgroup;public class Fragment1 extends Fragment {@Overridepublic view Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {return inflater.inflate ( R.layout.fragment1, container, false);}}
Rewrite the Oncreateview () method, herereturn Inflater.inflate (R.layout.fragment1, container, false);This sentence is the point
4. Create Class Fragment2 inheritance Fragment
Package Lgx.fram.framents;import Android.app.fragment;import Android.os.bundle;import android.view.LayoutInflater; Import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.textview;public class Fragment2 extends Fragment {@Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {return inflater.inflate ( R.layout.fragment2, container, false);} TextView TextView; Button button, button2, Button3; @Overridepublic void onactivitycreated (Bundle savedinstancestate) { Super.onactivitycreated (savedinstancestate); button = (button) getactivity (). Findviewbyid (R.id.button); button2 = ( button) getactivity (). Findviewbyid (r.id.button2); button3 = (button) getactivity (). Findviewbyid (R.id.button3); TextView = (TextView) getactivity (). Findviewbyid (R.id.fragment_text); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {textview.settext (button).GetText ());}); Button2.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Textview.settext ( Button2.gettext ());}); Button3.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Textview.settext ( Button3.gettext ());}});}}
button = (button) getactivity (). Findviewbyid (R.id.button);This way to get the controls above the fragment
The code inside the 5.activity_fragment.xml is like this.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/main_layout " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:baselinealigned=" false " android:orientation=" Horizontal "> <fragment android:id= "@+id/fragment1" android:name= "lgx.fram.framents.Fragment1" android:layout_width= " Match_parent " android:layout_height=" match_parent " android:layout_weight=" 1 "/> <fragment android:id= "@+id/fragment2" android:name= "Lgx.fram.framents.Fragment2" android:layout_width= " Match_parent " android:layout_height=" match_parent " android:layout_weight=" 1 "/></linearlayout >
Note: The android:name= in the control fragment "" is filled with the absolute path of your fragment class (the brain suddenly shorted out, is that said??). ), ID is used to mark fragment.
6.FragmentActivity is the simplest, just setcontentview, and no other changes have been made. Look underneath.
Package Lgx.fram.framents;import Android.app.activity;import Android.os.bundle;public class FragmentActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_fragment);}}
Here my whole little application is done. My fragment here is added to the activity through the layout file, and another way is to programmatically add fragment to the activity. Here I'm simply describing
The 1,2,3,4 above do not need to move.
The 5th step, activity_fragment.xml inside the code into the following
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/main_layout " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:baselinealigned=" false " android:orientation=" Horizontal "> </ Linearlayout>
You will find my knowledge removed two fragment, the whole linearlayout add in the ID
The 6th step, inside the note, has been written very clearly:
package lgx.fram.framents;import android.os.bundle;import android.app.Activity; Import Android.view.display;import android.view.menu;/** * * @author Lenovo Dynamic Add fragment is mainly divided into 4 steps: * 1. Get to Fragmentma Nager can be obtained directly through Getfragmentmanager in the activity. * 2. Open a transaction by calling the BeginTransaction method. * 3. Adding fragment to the container, typically implemented with the Replace method, requires passing in the ID of the container and an instance of fragment. * 4. Commit the transaction and invoke the Commit method commit. */public class Fragmentactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_fragment);D isplay Display = GetWindowManager ( ). Getdefaultdisplay (); if (Display.getwidth () > Display.getheight ()) {Fragment1 fragment1 = new Fragment1 (); Getfragmentmanager (). BeginTransaction (). replace (R.id.main_layout, fragment1). commit ();} else {Fragment2 Fragment2 = new Fragment2 (); Getfragmentmanager (). BeginTransaction (). replace (R.id.main_layout, Fragment2). commit ();}}
This code means that the screen displays different fragment. If this is a simulator test, press CTRL+F11.
I use the Replace method here, and one more way is to add ... Here is not to repeat, please go here to see Android Fragment basic Introduction
Complete...
The realization of communication between simple fragment