ImportAndroid.os.Bundle;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.support.v4.app.FragmentManager;Importandroid.support.v4.app.FragmentTransaction;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;Importandroid.widget.FrameLayout;ImportAndroid.widget.ImageView;Importandroid.widget.RelativeLayout;ImportAndroid.widget.TextView; Public classMainactivityextendsFragmentactivityImplementsonclicklistener{//defines 3 objects for a fragment PrivateFragment1 Fg1; PrivateFragment2 Fg2; PrivateFragment3 fg3; //frame Layout object, which is the container for storing fragment PrivateFramelayout flayout; //define three layouts for the bottom navigation bar each layout corresponds to an option button PrivateRelativelayout course_layout; PrivateRelativelayout found_layout; PrivateRelativelayout settings_layout; //define ImageView and TextView in the bottom navigation bar PrivateImageView Course_image; PrivateImageView Found_image; PrivateImageView Settings_image; PrivateTextView Course_text; PrivateTextView Settings_text; PrivateTextView Found_text; //define the color value to use Private intWhirt = 0xFFFFFFFF; Private intGray = 0xff7597b3; Private intBlue =0XFF0AB2FB; //Defining Fragmentmanager ObjectsFragmentmanager Fmanager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Fmanager= Getsupportfragmentmanager ();//get the manager to manage fragmentInitviews ();//initializing layout controls makes the code structure clearer } //complete initialization of the component Public voidinitviews () {course_image=(ImageView) Findviewbyid (r.id.course_image); Found_image=(ImageView) Findviewbyid (r.id.found_image); Settings_image=(ImageView) Findviewbyid (r.id.setting_image); Course_text=(TextView) Findviewbyid (R.id.course_text); Found_text=(TextView) Findviewbyid (R.id.found_text); Settings_text=(TextView) Findviewbyid (R.id.setting_text); Course_layout=(relativelayout) Findviewbyid (r.id.course_layout); Found_layout=(relativelayout) Findviewbyid (r.id.found_layout); Settings_layout=(relativelayout) Findviewbyid (r.id.setting_layout); Course_layout.setonclicklistener ( This);//Binding ListenersFound_layout.setonclicklistener ( This); Settings_layout.setonclicklistener ( This); } //overriding the onclick event@Override Public voidOnClick (view view) {Switch(View.getid ()) { CaseR.id.course_layout:setchioceitem (0); Break; CaseR.id.found_layout:setchioceitem (1); Break; CaseR.id.setting_layout:setchioceitem (2); Break; default: Break; } } //define a process after selecting an item Public voidSetchioceitem (intindex) { //Reset Option + Hide all fragmentFragmenttransaction transaction =fmanager.begintransaction (); Clearchioce (); //Initialize the entire layout every time a response event is obtainedhidefragments (transaction); Switch(index) { Case0: Course_image.setimageresource (r.drawable.ic_tabbar_course_pressed); Course_text.settextcolor (blue); if(FG1 = =NULL) { //if Fg1 is empty, then one is created and added to the interface this code is used to initialize the creation of the fragmentFG1 =NewFragment1 (); Transaction.add (R.id.content, FG1); } Else { //If the messagefragment is not empty, it is displayed directlytransaction.show (FG1); } Break; Case1: Found_image.setimageresource (r.drawable.ic_tabbar_found_pressed); Found_text.settextcolor (blue); if(Fg2 = =NULL) { //if Fg1 is empty, create one and add it to the interfaceFG2 =NewFragment2 (); Transaction.add (R.id.content, FG2); } Else { //If the messagefragment is not empty, it is displayed directlytransaction.show (FG2); } Break; Case2: Settings_image.setimageresource (r.drawable.ic_tabbar_settings_pressed); Settings_text.settextcolor (blue); if(FG3 = =NULL) { //if Fg1 is empty, create one and add it to the interfaceFG3 =NewFragment3 (); Transaction.add (R.id.content, FG3); } Else { //If the messagefragment is not empty, it is displayed directlytransaction.show (FG3); } Break; } transaction.commit ();//Commit a transaction } //Hide all the fragment, avoid fragment confusion if there is no hidden words after the run results will be out first click the previous option does not display the corresponding layout correctly Private voidhidefragments (fragmenttransaction transaction) {if(Fg1! =NULL) {transaction.hide (FG1); } if(Fg2! =NULL) {transaction.hide (FG2); } if(Fg3! =NULL) {transaction.hide (FG3); } } //define a method to reset all options Public voidClearchioce () {course_image.setimageresource (r.drawable.ic_tabbar_course_normal); Course_layout.setbackgroundcolor (Whirt); Course_text.settextcolor (gray); Found_image.setimageresource (R.drawable.ic_tabbar_found_normal); Found_layout.setbackgroundcolor (Whirt); Found_text.settextcolor (gray); Settings_image.setimageresource (R.drawable.ic_tabbar_settings_normal); Settings_layout.setbackgroundcolor (Whirt); Settings_text.settextcolor (gray); }}
ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classFragment1extendsFragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V Iew= Inflater.inflate (R.layout.fg1, container,false); returnview; }}
Android fragment parsing and UI bottom navigation bar instances