android - Open DrawerLayout, perform action, then close it -
so want animate drawerlayout in such way opened, item added menu in it, closed.
i tried great deal of things, problem there no delay between "open" , "close" commands (or @ least not noticeable one) , animation never triggers.
i don't want use 'post delayed' arbitrary length because seems dirty. i've tried manually overriding "ondraweropened()" in listener - worked, can't close after action performed because can't close within (meaning, if 'closedrawer(layout)' method invoked sub-thread of listener itself, it'll runtime error).
so have is:
_drawertoggle = new actionbardrawertoggle(mainactivity.this, _drawerlayout, r.drawable.ic_drawer, r.string.draweropen, r.string.drawerclose) { /** called when drawer has settled in closed state. */ public void ondrawerclosed(view view) { getactionbar().settitle(_title); } /** called when drawer has settled in open state. */ public void ondraweropened(view drawerview) { myaction(); //my action //if try invoke 'drawerclose()' here or in action, runtime error occurs } }; _drawerlayout.setdrawerlistener(_drawertoggle); _drawerlayout.opendrawer(_mainframe); //if invoke 'drawerclose()' entire thing fast , animations don't trigger
i tried 'getanimation()' on drawerlayout returns null can't use post delayed action.
anyone have idea how approach this? should used arbitrary delay , hope looks same on devices? there way close drawer within listener? in advance!
calling closedrawer()
isn't working in ondraweropened()
because, well, drawer hasn't finished opening. if watch ondrawerstatechanged()
you'll notice state still changes after ondraweropened()
called.
i tried closing drawer when ondrawerstatechanged()
said drawer idle, still crashed. final solution looks, said, hacky. works, , looks smooth. should placed in ondraweropened()
new thread() { @override public void run() { systemclock.sleep(500); mainactivity.this.runonuithread(new runnable() { @override public void run() { mdrawerlayout.closedrawers(); } }); } }.start();
this little better if use sort or thread manager, 1 posted here
https://gist.github.com/bclymer/6708605
disclaimer, that's utility class. use in of apps though, it's convenient. utility code like
threadmanager.delayonmainthread(new runnable() { @override public void run() { mdrawerlayout.closedrawers(); } }, 500);
Comments
Post a Comment