android - navigation drawer does not show main layout when app start -
my navigation drawer works perfect every time run program drawer opened default instead of main layout. navigation drawer should opened when clicked on drawer button.
public class mainactivity extends activity { private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; customdraweradapter adapter; list<draweritem> datalist; @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // initializing datalist = new arraylist<draweritem>(); mtitle = mdrawertitle = gettitle(); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); mdrawerlayout.setdrawershadow(r.drawable.drawer_shadow, gravitycompat.start); // add drawer item datalist // add drawer item datalist datalist.add(new draweritem("message", r.drawable.ic_action_email)); datalist.add(new draweritem("likes", r.drawable.ic_action_good)); datalist.add(new draweritem("games", r.drawable.ic_action_gamepad)); datalist.add(new draweritem("lables", r.drawable.ic_action_labels)); datalist.add(new draweritem("search", r.drawable.ic_action_search)); datalist.add(new draweritem("cloud", r.drawable.ic_action_cloud)); datalist.add(new draweritem("camara", r.drawable.ic_action_camera)); datalist.add(new draweritem("video", r.drawable.ic_action_video)); datalist.add(new draweritem("groups", r.drawable.ic_action_group)); datalist.add(new draweritem("import & export", r.drawable.ic_action_import_export)); datalist.add(new draweritem("about", r.drawable.ic_action_about)); datalist.add(new draweritem("settings", r.drawable.ic_action_settings)); datalist.add(new draweritem("help", r.drawable.ic_action_help)); adapter = new customdraweradapter(this, r.layout.custom_drawer_item,datalist); mdrawerlist.setadapter(adapter); mdrawerlist.setonitemclicklistener(new draweritemclicklistener()); getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_drawer, r.string.drawer_open, r.string.drawer_close) { public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); invalidateoptionsmenu(); // creates call // onprepareoptionsmenu() } public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); invalidateoptionsmenu(); // creates call // onprepareoptionsmenu() } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { selectitem(0); } } public void selectitem(int possition) { fragment fragment = null; bundle args = new bundle(); switch (possition) { case 0: fragment = new fragmentone(); args.putstring(fragmentone.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentone.image_resource_id, datalist.get(possition) .getimgresid()); break; case 1: fragment = new fragmenttwo(); args.putstring(fragmenttwo.item_name, datalist.get(possition) .getitemname()); args.putint(fragmenttwo.image_resource_id, datalist.get(possition) .getimgresid()); break; case 2: fragment = new fragmentthree(); args.putstring(fragmentthree.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentthree.image_resource_id, datalist.get(possition) .getimgresid()); break; case 3: fragment = new fragmentone(); args.putstring(fragmentone.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentone.image_resource_id, datalist.get(possition) .getimgresid()); break; case 4: fragment = new fragmenttwo(); args.putstring(fragmenttwo.item_name, datalist.get(possition) .getitemname()); args.putint(fragmenttwo.image_resource_id, datalist.get(possition) .getimgresid()); break; case 5: fragment = new fragmentthree(); args.putstring(fragmentthree.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentthree.image_resource_id, datalist.get(possition) .getimgresid()); break; case 6: fragment = new fragmentone(); args.putstring(fragmentone.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentone.image_resource_id, datalist.get(possition) .getimgresid()); break; case 7: fragment = new fragmenttwo(); args.putstring(fragmenttwo.item_name, datalist.get(possition) .getitemname()); args.putint(fragmenttwo.image_resource_id, datalist.get(possition) .getimgresid()); break; case 8: fragment = new fragmentthree(); args.putstring(fragmentthree.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentthree.image_resource_id, datalist.get(possition) .getimgresid()); break; case 9: fragment = new fragmentone(); args.putstring(fragmentone.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentone.image_resource_id, datalist.get(possition) .getimgresid()); break; case 10: fragment = new fragmenttwo(); args.putstring(fragmenttwo.item_name, datalist.get(possition) .getitemname()); args.putint(fragmenttwo.image_resource_id, datalist.get(possition) .getimgresid()); break; case 11: fragment = new fragmentthree(); args.putstring(fragmentthree.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentthree.image_resource_id, datalist.get(possition) .getimgresid()); break; case 12: fragment = new fragmentone(); args.putstring(fragmentone.item_name, datalist.get(possition) .getitemname()); args.putint(fragmentone.image_resource_id, datalist.get(possition) .getimgresid()); break; default: break; } fragment.setarguments(args); fragmentmanager frgmanager = getfragmentmanager(); frgmanager.begintransaction().replace(r.id.content_frame, fragment) .commit(); mdrawerlist.setitemchecked(possition, true); settitle(datalist.get(possition).getitemname()); mdrawerlayout.closedrawer(mdrawerlist); } @override public void settitle(charsequence title) { mtitle = title; getactionbar().settitle(mtitle); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @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; } return false; } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggles mdrawertoggle.onconfigurationchanged(newconfig); } @suppresswarnings("unused") private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position); } } }
a post made couple of days ago appears use same nav drawer template. there, problem solved removing these lines of code:
if (savedinstancestate == null) { selectitem(0); }
as first item in list being automatically selected. try deleting section , see happens. this post referring well.
Comments
Post a Comment