A total of 4 fragment, namely contact (contacts), Friends (Friends), search (Find), more (more). Use the same layout, each fragment has four internal buttons, you can switch to the other 3 fragment.
Now consider only in contact, implement point 4 (in fact, 3), switch to the other 3 fragment.
Now, when you click the button in Contactfragment, the toast responds, but fragment does not jump. Here's what's going on, here's the code for two classes.
This is contactfragment.
Public classContactfragment extends Fragment implements Onclicklistener {mainactivity activity=(mainactivity) getactivity (); View View=NULL; Button btn_inner_contact; Button Btn_inner_friends; Button Btn_inner_search; Button Btn_inner_more; @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {view /c1>= Inflater.inflate (r.layout.frag_content, container,false); TextView Textview_hint=(TextView) view. Findviewbyid (R.id.textview_hint); Textview_hint.settext (" Contact"); InitComponent (); returnview; } Private voidinitcomponent () {btn_inner_contact=(Button) view. Findviewbyid (R.id.button_inner_contact); Btn_inner_friends=(Button) view. Findviewbyid (R.id.button_inner_friends); Btn_inner_search=(Button) View.findviewbyid (R.id.button_inner_search); Btn_inner_more=(Button) View.findviewbyid (R.id.button_inner_more); Btn_inner_contact.setonclicklistener ( This); Btn_inner_friends.setonclicklistener ( This); Btn_inner_search.setonclicklistener ( This); Btn_inner_more.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Fragmentmanager fm=Activity.getfragmentmanager (); Fragmenttransaction ft=fm.begintransaction (); Switch(V.getid ()) { CaseR.id.button_inner_contact:toast.maketext (Getactivity (),"Click the Internal contact button", Toast.length_short). Show (); if(Activity.contactfragment = =NULL) {activity.contactfragment=Newcontactfragment (); } ft.replace (R.id.framelayout_content, activity.contactfragment); Break; CaseR.id.button_inner_friends:toast.maketext (Getactivity (),"Click on the Internal Friends button", Toast.length_short)if(Activity.friendsfragment = =NULL) {activity.friendsfragment=Newfriendsfragment (); } ft.replace (R.id.framelayout_content, activity.friendsfragment); Break; CaseR.id.button_inner_more:if(Activity.morefragment = =NULL) {activity.morefragment=Newmorefragment (); } ft.replace (R.id.framelayout_content, activity.morefragment); Break; CaseR.id.button_inner_search:if(Activity.searchfragment = =NULL) {activity.searchfragment=Newsearchfragment (); } ft.replace (R.id.framelayout_content, activity.searchfragment); Break; default: Break; } ft.commit (); } }
This is the Mainactivity class, and the four buttons in mainactivity and their response time are four buttons outside of fragment, and the inner button inside is okay
Public classMainactivity extends Activity implements Onclicklistener { Publiccontactfragment contactfragment; Publicfriendsfragment friendsfragment; Publicmorefragment morefragment; Publicsearchfragment searchfragment; PrivateButton btn_contact; PrivateButton btn_friends; PrivateButton Btn_search; PrivateButton Btn_more; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initcomponet (); Initview (); } Private voidInitview () {contactfragment=Newcontactfragment (); Fragmentmanager FM=Getfragmentmanager (); Fragmenttransaction ft=fm.begintransaction (); Ft.add (R.id.framelayout_content, contactfragment); Ft.commit (); } Private voidinitcomponet () {btn_contact=(Button) Findviewbyid (r.id.button_contact); Btn_friends=(Button) Findviewbyid (r.id.button_friends); Btn_more=(Button) Findviewbyid (R.id.button_more); Btn_search=(Button) Findviewbyid (R.id.button_search); Btn_contact.setonclicklistener ( This); Btn_friends.setonclicklistener ( This); Btn_more.setonclicklistener ( This); Btn_search.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Fragmentmanager fm=Getfragmentmanager (); Fragmenttransaction ft=fm.begintransaction (); Switch(V.getid ()) { Caser.id.button_contact:if(Contactfragment = =NULL) {contactfragment=Newcontactfragment (); } ft.replace (R.id.framelayout_content, contactfragment); Break; CaseR.id.button_friends:if(Friendsfragment = =NULL) {friendsfragment=Newfriendsfragment (); } ft.replace (R.id.framelayout_content, friendsfragment); Break; CaseR.id.button_more:if(Morefragment = =NULL) {morefragment=Newmorefragment (); } ft.replace (R.id.framelayout_content, morefragment); Break; CaseR.id.button_search:if(Searchfragment = =NULL) {searchfragment=Newsearchfragment (); } ft.replace (R.id.framelayout_content, searchfragment); Break; default: Break; } ft.commit (); }}
I tried, put mainactivity activity= (mainactivity) getactivity (), this line of code put in the OnClick method, it is OK!
Android fragment jump to another fragment