java - How to open any fragment after Navigation Drawer closed -
my fragment opens after clicking navigation drawer item. want open after navigation drawer has closed.
how open fragment after drawer closed in navigation drawer support library?
private void initnavigationview() { navigationview = (navigationview) findviewbyid(r.id.navigation_view); drawerlayout = (drawerlayout) findviewbyid(r.id.navigation_drawer); drawertoggle = new actionbardrawertoggle(this, drawerlayout, r.string.nav_open, r.string.nav_close) { @override public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); invalidateoptionsmenu(); } @override public void ondrawerclosed(view drawerview) {super.ondrawerclosed(drawerview) } }; drawerlayout.post(new runnable() { @override public void run() { drawertoggle.syncstate(); } }); drawertoggle.setdrawerindicatorenabled(true); drawerlayout.setdrawerlistener(drawertoggle); navigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() { @override public boolean onnavigationitemselected(menuitem menuitem) { switch (menuitem.getitemid()) { case r.id.tentang: break; case r.id.keluar: finish(); break; } selectdraweritem(menuitem); return true; } private void selectdraweritem(menuitem menuitem) { fragment fragment = null; class fragmentclass = null; switch (menuitem.getitemid()) { case r.id.fragment_home: fragmentclass = com.wahyuapps.benux.fragment.fragmenthome.class; break; case r.id.fragment_distro_linux: fragmentclass = com.wahyuapps.benux.fragment.fragmentdistrolinux.class; break; } try { fragment = (fragment)fragmentclass.newinstance(); }catch(exception e){ e.printstacktrace(); } fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.fragment_content, fragment).commit(); menuitem.setchecked(true); settitle(menuitem.gettitle()); drawerlayout.closedrawers(); } }); }
what code put on this? how open fragment when drawer closed? on case, fragment got opening after clicking item. want opening fragment delayed after drawer closed.
android.support.v4.app.fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); testingfragment hello = new testingfragment(); fragmenttransaction.add(r.id.mainlayout, hello, "hello"); fragmenttransaction.commit();
with can call fragment...and r.id.mainlayout rootlayout of activity.
edit 2
to open fragment after delay
handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.fragment_content, fragment).commit(); menuitem.setchecked(true); settitle(menuitem.gettitle()); } }, 1000);
choose import handler - import android.os.handler;
Comments
Post a Comment