Android one activity inside put multiple Fragment to achieve click to Toggle tab page effect

Source: Internet
Author: User

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 buttons
PrivateTextView 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 fragment
Private 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.