Fragment Implementing Tab
First, change the Viewpager tag in the Activity_main.xml file to the fragment tag.
1 <framelayout2 android:id= "@+id/id_content"3 android:layout_width= "Fill_ Parent "4 android:layout_height=" 0DP "5 android:layout_weight=" 1 ">6 </FrameLayout>
View Code
The other XML files are the same as in the previous one, and they are not duplicated.
Unlike the previous Viewpager, we also create a corresponding fragment class for each tab and populate the view in the Fragment class
As follows:
Packagecom.imooc.tab02;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classWeixinfragmentextendsfragment{@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {returnInflater.inflate (R.LAYOUT.TAB01, container,false); }}View Code
Finally look at the Mainactivity class
Packagecom.imooc.tab02;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.support.v4.app.FragmentManager;Importandroid.support.v4.app.FragmentTransaction;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.Window;ImportAndroid.widget.ImageButton;Importandroid.widget.LinearLayout; Public classMainactivityextendsFragmentactivityImplementsOnclicklistener {PrivateLinearLayout mtabweixin; PrivateLinearLayout MTABFRD; PrivateLinearLayout mtabaddress; PrivateLinearLayout mtabsettings; PrivateImageButton mimgweixin; PrivateImageButton MIMGFRD; PrivateImageButton mimgaddress; PrivateImageButton mimgsettings; PrivateFragment mTab01; PrivateFragment mTab02; PrivateFragment mTab03; PrivateFragment mTab04; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Initview (); Initevent (); Setselect (0); } Private voidinitevent () {Mtabweixin.setonclicklistener ( This); Mtabfrd.setonclicklistener ( This); Mtabaddress.setonclicklistener ( This); Mtabsettings.setonclicklistener ( This); } Private voidInitview () {mtabweixin=(LinearLayout) Findviewbyid (r.id.id_tab_weixin); MTABFRD=(LinearLayout) Findviewbyid (R.ID.ID_TAB_FRD); Mtabaddress=(LinearLayout) Findviewbyid (r.id.id_tab_address); Mtabsettings=(LinearLayout) Findviewbyid (r.id.id_tab_settings); Mimgweixin=(ImageButton) Findviewbyid (r.id.id_tab_weixin_img); MIMGFRD=(ImageButton) Findviewbyid (r.id.id_tab_frd_img); Mimgaddress=(ImageButton) Findviewbyid (r.id.id_tab_address_img); Mimgsettings=(ImageButton) Findviewbyid (r.id.id_tab_settings_img); } Private voidSetselect (inti) {Fragmentmanager fm=Getsupportfragmentmanager (); Fragmenttransaction Transaction=fm.begintransaction (); Hidefragment (transaction); Switch(i) { Case0: if(MTAB01 = =NULL) {mTab01=Newweixinfragment (); Transaction.add (R.id.id_content, MTAB01); } Else{transaction.show (MTAB01); } mimgweixin.setimageresource (r.drawable.tab_weixin_pressed); Break; Case1: if(MTAB02 = =NULL) {mTab02=Newfrdfragment (); Transaction.add (R.id.id_content, MTAB02); } Else{transaction.show (MTAB02); } mimgfrd.setimageresource (r.drawable.tab_find_frd_pressed); Break; Case2: if(MTAB03 = =NULL) {mTab03=Newaddressfragment (); Transaction.add (R.id.id_content, MTAB03); } Else{transaction.show (MTAB03); } mimgaddress.setimageresource (r.drawable.tab_address_pressed); Break; Case3: if(MTab04 = =NULL) {mTab04=Newsettingfragment (); Transaction.add (R.id.id_content, mTab04); } Else{transaction.show (MTAB04); } mimgsettings.setimageresource (r.drawable.tab_settings_pressed); Break; default: Break; } transaction.commit (); } Private voidhidefragment (fragmenttransaction transaction) {if(MTab01! =NULL) {transaction.hide (MTAB01); } if(MTAB02! =NULL) {transaction.hide (MTAB02); } if(MTab03! =NULL) {transaction.hide (MTAB03); } if(MTab04! =NULL) {transaction.hide (MTAB04); }} @Override Public voidOnClick (View v) {Resetimgs (); Switch(V.getid ()) { CaseR.id.id_tab_weixin:setselect (0); Break; CaseR.id.id_tab_frd:setselect (1); Break; CaseR.id.id_tab_address:setselect (2); Break; CaseR.id.id_tab_settings:setselect (3); Break; default: Break; } } Private voidResetimgs () {mimgweixin.setimageresource (r.drawable.tab_weixin_normal); Mimgfrd.setimageresource (R.drawable.tab_find_frd_normal); Mimgaddress.setimageresource (R.drawable.tab_address_normal); Mimgsettings.setimageresource (R.drawable.tab_settings_normal); }}View Code
"Imooc Learning Notes" a variety of App main Interface tab implementation method (II)