Baseactivity is a class that implements some common variables and methods that can be temporarily empty
Public class extends Implements View.onclicklistener {
The following four buttonsPrivateTextView Mhomeview; PrivateTextView Mpondview; PrivateTextView Mmessageview; PrivateTextView Mmineview;
Layout-relative layouts contain 2 TextView with icons and text, respectively.PrivateRelativelayout mhomelayout; PrivateRelativelayout mpondlayout; PrivateRelativelayout mmessagelayout; PrivateRelativelayout mminelayout; Declares Fragmentmanager and four fragment as well as the current fragmentPrivateFragmentmanager FM; Privatehomefragment mhomefragment; PrivateFragment Mcommonfragmentone; Privatemessagefragment mmessagefragment; Privateminefragment mminefragment; PrivateFragment mcurrent; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.activity_home_layout);
Initializes the control variable Initview ();
Create the fragment mhomefragment of the homepage=Newhomefragment ();
Get the Fragment manager, the Activity base class inside the FM defined=Getfragmentmanager ();
Fragmenttransaction fragmenttransaction=fm.begintransaction ();
Use homefragment to replace Content_layout layout fragmenttransaction.replace (r.id.content_layout, mhomefragment); Fragmenttransaction.commit (); } Private voidInitview () {
First initialize layout and assign a method of its click Mhomelayout=(relativelayout) Findviewbyid (R.id.home_layout_view); Mhomelayout.setonclicklistener ( This); Mpondlayout=(relativelayout) Findviewbyid (R.id.pond_layout_view); Mpondlayout.setonclicklistener ( This); Mmessagelayout=(relativelayout) Findviewbyid (R.id.message_layout_view); Mmessagelayout.setonclicklistener ( This); Mminelayout=(relativelayout) Findviewbyid (R.id.mine_layout_view); Mminelayout.setonclicklistener ( This);
Re-initialize the inside of the TextView because when you click on the background of TextView to change a picture
Note that there is an empty TextView, the background is an icon, click on the change is this icon Mhomeview=(TextView) Findviewbyid (R.id.home_image_view); Mpondview=(TextView) Findviewbyid (R.id.fish_image_view); Mmessageview=(TextView) Findviewbyid (R.id.message_image_view); Mmineview=(TextView) Findviewbyid (R.id.mine_image_view); Mhomeview.setbackgroundresource (r.drawable.comui_tab_home_selected); }
Use Fragmenttransaction to hide fragmentPrivate voidhidefragment (Fragment Fragment, fragmenttransaction ft) {if(Fragment! =NULL) {ft.hide (fragment); }} @Override Public voidOnClick (View v) {fragmenttransaction fragmenttransaction=fm.begintransaction (); Switch(V.getid ()) { CaseR.id.home_layout_view://Not required, can be deleted
Changestatusbarcolor (r.color.color_fed952);
Change the background picture Mhomeview.setbackgroundresource (r.drawable.comui_tab_home_selected); Mpondview.setbackgroundresource (R.drawable.comui_tab_pond); Mmessageview.setbackgroundresource (R.drawable.comui_tab_message); Mmineview.setbackgroundresource (R.drawable.comui_tab_person); Hide other fragment hidefragment (Mcommonfragmentone, fragmenttransaction); Hidefragment (Mmessagefragment, fragmenttransaction); Hidefragment (Mminefragment, fragmenttransaction); if(Mhomefragment = =NULL) {
Add a new fragment mhomefragment=Newhomefragment (); Fragmenttransaction.add (R.id.content_layout, mhomefragment); } Else {
Show current fragment Mcurrent=mhomefragment; Fragmenttransaction.show (mhomefragment); } Break; CaseR.id.message_layout_view:changestatusbarcolor (R.COLOR.COLOR_E3E3E3); Mmessageview.setbackgroundresource (r.drawable.comui_tab_message_selected); Mhomeview.setbackgroundresource (R.drawable.comui_tab_home); Mpondview.setbackgroundresource (R.drawable.comui_tab_pond); Mmineview.setbackgroundresource (R.drawable.comui_tab_person); Hidefragment (Mcommonfragmentone, fragmenttransaction); Hidefragment (Mhomefragment, fragmenttransaction); Hidefragment (Mminefragment, fragmenttransaction); if(Mmessagefragment = =NULL) {mmessagefragment=Newmessagefragment (); Fragmenttransaction.add (R.id.content_layout, mmessagefragment); } Else{mcurrent=mmessagefragment; Fragmenttransaction.show (mmessagefragment); } Break; CaseR.id.mine_layout_view:changestatusbarcolor (R.COLOR.COLOR_00FFFFFF); Mmineview.setbackgroundresource (r.drawable.comui_tab_person_selected); Mhomeview.setbackgroundresource (R.drawable.comui_tab_home); Mpondview.setbackgroundresource (R.drawable.comui_tab_pond); Mmessageview.setbackgroundresource (R.drawable.comui_tab_message); Hidefragment (Mcommonfragmentone, fragmenttransaction); Hidefragment (Mmessagefragment, fragmenttransaction); Hidefragment (Mhomefragment, fragmenttransaction); if(Mminefragment = =NULL) {mminefragment=Newminefragment (); Fragmenttransaction.add (R.id.content_layout, mminefragment); } Else{mcurrent=mminefragment; Fragmenttransaction.show (mminefragment); } Break; } fragmenttransaction.commit (); }}
Android one activity inside put multiple Fragment to achieve click to Toggle tab page effect