This section learns fragment, fragment is the new API introduced by Android3.0. The original intention was to fit a tablet with a large screen. Fragment represents the sub-module of activity, so fragment can be understood as a fragment of activity. This section learns how to create fragment.
1: First create a layout file for display, the left is a button to switch fragment, and the right is fragment to display the content
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "${relativepackage}.${activityclass}" > <linearlayout android:layout_width= "wrap_content" and roid:layout_height= "match_parent" android:orientation= "vertical" > <button Android:id= "@+id/bt_1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Fragement1" android:onclick= "Red"/> <button android:i D= "@+id/bt_2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an droid:text= "Fragement2" android:onclick= "green"/> <button android:id= "@+id /bt_3 "Android:layout_width=" Wrap_coNtent "android:layout_height=" wrap_content "android:text=" Fragement3 "android:onclick=" bl UE "/> </LinearLayout> <framelayout android:id=" @+id/fl "Android:layout_we ight= "1" android:layout_width= "0DP" android:layout_height= "match_parent" > </framela Yout> </LinearLayout>
2: Create a fragment subclass that inherits the fragment class.
FRAGMENT01:
public class Fragement01 extends Fragment {///Return View object will be displayed as FRAGEMENT1 content on the interface @overridepublic view Oncreateview ( Layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {View v = inflater.inflate (R.layout.fragement1 , null); return v;}}
FRAGMENT02:
public class Fragement02 extends Fragment {///Return View object will be displayed as Fragement2 content on the interface @overridepublic view Oncreateview ( Layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {View v = inflater.inflate (R.layout.fragement2 , null); return v;}}
Fragment03:
public class Fragement03 extends Fragment {///Return View object will be displayed as FRAGEMENT3 content on the interface @overridepublic view Oncreateview ( Layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {View v = inflater.inflate (R.layout.fragement3 , null); return v;}}
Because each fragment must correspond to a layout file, 3 fragment layout files are required
Fragment01 Layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" vertical " android:background=" # FF0000 " > <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:text=" This is Fragement1 " android:textsize=" 25SP " /></linearlayout>
Fragment02 Layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" vertical " android:background=" # 00ff00 " > <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:text=" This is Fragement2 " android:textsize=" 25SP " /></linearlayout>
Fragment03 Layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" vertical " android:background=" # 0000FF " > <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:text=" This is Fragement3 " android:textsize=" 25SP " /></linearlayout>
The rest is the activity code:
public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {s Uper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); public void Red (View v) {///fragement01 interface displayed in layout file//1: First get fragement Fragement01 fragement01 = n EW Fragement01 (); 2: Get fragement manager Fragmentmanager Fmanager = Getfragmentmanager (); 3: Open things fragmenttransaction ft = fmanager.begintransaction (); 4: Display the content to the interface Ft.replace (R.ID.FL, fragement01); 5: Submit Ft.commit (); } public void Green (View v) {///fragement02 interface displayed in layout file//1: First get fragement Fragement02 fragement02 = New Fragement02 (); 2: Get fragement manager Fragmentmanager Fmanager = Getfragmentmanager (); 3: Open things fragmenttransaction ft = fmanager.begintransaction (); 4: Display the content to the interface Ft.replace (R.ID.FL, fragement02); 5: Submit Ft.commit (); } public void Blue (VieW v) {//fragement03 interface displayed in layout file//1: First get fragement Fragement03 fragement03 = new Fragement03 (); 2: Get fragement manager Fragmentmanager Fmanager = Getfragmentmanager (); 3: Open things fragmenttransaction ft = fmanager.begintransaction (); 4: Display the content to the interface Ft.replace (R.ID.FL, fragement03); 5: Submit Ft.commit (); }}
Run to see the effect:
In fact, fragment is a few steps, remember that you can.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The creation of Android learning fragment