android - How to change the menu according to the fragment using navigation drawer -
i'm using navigationdrawer main activity, when go fragment want modify menu, putting arrow home , hide search icon of navbar, i'm having 2 problems here, can hide search icon when change fragment, when i'm navigationmain should visible again, not happening, , if in fragment navigation bar must removed , arrow must appear can home, i've tried far:
public class navigationmain extends actionbaractivity{ private drawerlayout mdrawerlayout; private listview mdrawerlist; private android.support.v7.app.actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; private string[] mplanettitles; static toolbar toolbar; textview toolbartitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.navigation_main); mplanettitles = getresources().getstringarray(r.array.planets_array); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); toolbar = (toolbar) findviewbyid(r.id.my_awesome_toolbar); toolbartitle = (textview) findviewbyid(r.id.titletool); mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mplanettitles)); mdrawerlist.setonitemclicklistener(new draweritemclicklistener()); mdrawertoggle = new android.support.v7.app.actionbardrawertoggle( this, /* host activity */ mdrawerlayout, /* drawerlayout object */ toolbar, /* nav drawer image replace 'up' caret */ r.string.drawer_open, /* "open drawer" description accessibility */ r.string.drawer_close /* "close drawer" description accessibility */ ) { public void ondrawerclosed(view view) { //getsupportactionbar().settitle(mtitle); toolbartitle.settext(mtitle); getsupportactionbar().setdisplayshowtitleenabled(false); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } public void ondraweropened(view drawerview) { //getsupportactionbar().settitle(mdrawertitle); toolbartitle.settext(mdrawertitle); getsupportactionbar().setdisplayshowtitleenabled(false); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } }; setsupportactionbar(toolbar); mdrawerlayout.setdrawerlistener(mdrawertoggle); getsupportactionbar().setdisplayshowtitleenabled(false); getsupportactionbar().setdisplayhomeasupenabled(true); toolbartitle.settext("nav"); toolbar.inflatemenu(r.menu.main); mdrawertoggle.syncstate(); if (savedinstancestate == null) { selectitem(0); } } @override public boolean oncreateoptionsmenu(menu menu) { return super.oncreateoptionsmenu(menu); } /* called whenever call invalidateoptionsmenu() */ @override public boolean onprepareoptionsmenu(menu menu) { // if nav drawer open, hide action items related content view return super.onprepareoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // action bar home/up action should open or close drawer. // actionbardrawertoggle take care of this. if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle action buttons switch (item.getitemid()) { case r.id.action_websearch: // create intent perform web search planet intent intent = new intent(intent.action_web_search); intent.putextra(searchmanager.query, toolbartitle.gettext()); // catch event there's no activity handle intent if (intent.resolveactivity(getpackagemanager()) != null) { startactivity(intent); } else { toast.maketext(this, r.string.app_not_available, toast.length_long).show(); } return true; default: return super.onoptionsitemselected(item); } } /* click listner listview in navigation drawer */ private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position); } } private void selectitem(int position) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragment fragment = new instituicoesfragment(); switch (position) { case 0: fragmentmanager.begintransaction().replace(r.id.content_frame, fragment).commit(); break; } // load conten here toast.maketext(navigationmain.this, "position" + position, toast.length_long).show(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); mdrawerlayout.closedrawer(mdrawerlist); } @override public void settitle(charsequence title) { mtitle = title; //getsupportactionbar().settitle(mtitle); toolbartitle.settext(mtitle); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggls mdrawertoggle.onconfigurationchanged(newconfig); } @override public void onbackpressed() { super.onbackpressed(); } }
in fragment add following:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sethasoptionsmenu(true); } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { inflater.inflate(r.menu.main, menu); menu.removeitem(r.id.action_websearch); super.oncreateoptionsmenu(menu, inflater); }
i used method : inflate menu actionbar items in. in activity oncreateoptionsmenu() check wich fragment displayed , set visibility of items accordingly. in onsectionattached method call invalidateoptionsmenu(); reload oncreateoptionsmenu can have different menu items on each fragment selected
for checking use boolean flags , if flag true means fragment n view, like:
viewing first fragment:
ft.replace(r.id.container, firstfragment); firstfragmentison=true; secondfragmentison=false;
viewing second fragment:
ft.replace(r.id.container, secondfragment); firstfragmentison=false; secondfragmentison=true;
and in oncreateoptionsmenu:
menuitem item1=menu.finditem(r.id.item1); menuitem item2=menu.finditem)(r.id.item2); if(firstfragmentison) { item1.setvisible(true); item2.setvisible(false); } if(secondfragmentison) { item1.setvisible(false); item2.setvisible(true); }
Comments
Post a Comment