Fragment is used more and more in the actual project development, now briefly introduce
Layout file:
<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 "Android:orien tation= "Vertical" tools:context= ". Mainactivity "> <framelayout android:id=" @+id/layout_content "android:layout_width=" Match_parent " android:layout_height= "0dip" android:layout_weight= "1.0"/> <radiogroup android:id= "@+id/main _radio "android:layout_width=" fill_parent "android:layout_height=" 50DP "android:layout_gravity=" Botto M "android:paddingtop=" 8DP "android:background=" @drawable/bottom_tab_bg "android:gravity=" Center_vert ical "android:orientation=" horizontal "> <radiobutton android:id=" @+id/rb_1 "sty le= "@style/main_tab_bottom" android:layout_width= "wrap_content" android:layout_height= "50DP" Android:drawabletop= "@drawable/icon_function"/> <radiobutton android:id= "@+id/rb_2" style= "@ Style/main_tab_bottom "android:layout_width=" wrap_content "android:layout_height=" 50DP "a ndroid:drawabletop= "@drawable/icon_newscenter"/> <radiobutton android:id= "@+id/rb_3" s tyle= "@style/main_tab_bottom" android:layout_width= "wrap_content" android:layout_height= "50DP" android:drawabletop= "@drawable/icon_govaffairs"/> <radiobutton android:id= "@+id/rb_4" style= "@style/main_tab_bottom" android:layout_width= "wrap_content" android:layout_height= "50DP "Android:drawabletop=" @drawable/icon_setting "/> <radiobutton android:id=" @+id/rb_5 " style= "@style/main_tab_bottom" android:layout_width= "Wrap_content" android:layout_height= " 50DP "Android:d rawabletop= "@drawable/icon_smartservice"/> </RadioGroup></LinearLayout>
Java file:
Package Com.succ7.fragmenttest;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentstatepageradapter;import Android.view.window;import Android.widget.framelayout;import Android.widget.radiogroup;import Android.widget.radiogroup.oncheckedchangelistener;public class Mainactivity extends Fragmentactivity {private Framelayout layout_content;/** * Default display of fragment */private int index = 0;private int current_index = r.id.rb_1; @Overrideprot ected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature ( Window.feature_no_title); Setcontentview (r.layout.activity_main); layout_content = (framelayout) findViewById ( R.id.layout_content); Radiogroup Main_radio = (radiogroup) Findviewbyid (R.id.main_radio); Main_radio.setoncheckedchangelistener (new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged (radiogroup group, int checkedid) {switch (Checkedid) { Case R.ID.rb_1:index = 0;break;case R.id.rb_2:index = 1;break;case R.id.rb_3:index = 2;break;case R.id.rb_4:index = 3;break;case R . Id.rb_5:index = 4;break;} This is the initialization of fragmentfragment fragment = (fragment) Fragments.instantiateitem (layout_content, index);// The default selection is No. 0 fragmentfragments.setprimaryitem (layout_content, 0, fragment);//fragment commits the update transaction fragments.finishupdate ( layout_content);}); Main_radio.check (Current_index);} Getsupportfragmentmanager () This method is in the fragmentactivity inside the fragmentstatepageradapter fragments = new Fragmentstatepageradapter (Getsupportfragmentmanager ()) {///several fragment return several @overridepublic int getcount () {return 5;} @Overridepublic Fragment getItem (int arg0) {Fragment Fragment = Null;switch (arg0) {case 0:fragment = new Fragment1 (); Brea K;case 1:fragment = new Fragment2 () break;case 2:fragment = new Fragment3 (); Break;case 3:fragment = new Fragment4 (); break; Case 4:fragment = new Fragment5 (); return fragment;}};}
Fragment1,fragment2-5 Code:
Package Com.succ7.fragmenttest;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.imageview;public class Fragment1 extends Fragment {@Overridepublic View oncreateview (layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {ImageView ImageView = new ImageView (getactivity ()); Imageview.setbackgroundresource (R.DRAWABLE.G1); return ImageView;} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate);} @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);} @Overridepublic void Setmenuvisibility (Boolean menuvisible) {super.setmenuvisibility (menuvisible); if (GetView ()! = NULL) {GetView (). setvisibility (menuvisible? View.VISIBLE:View.INVISIBLE);} /*if (menuvisible) {GetView (). setvisibility (view.visible);} Else{getview (). setvisibility (view.invisible);} */}}
Introduction to the use of fragment "Android"