java - ViewFlipper: flipping at random time intervals using random children -
i want create menu "flips" @ random time intervals between 5 children of viewflipper in random order.
i tried following code , can system.out.println display debugging message logged in logcat @ random time intervals that's working. however, screen in emulator black.
when use setdisplayedchild method in "oncreate" method fixed int, works fine. can me this? many!
public class flippertest extends activity { int randomtime; int randomchild; viewflipper fliptest; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_beat_the_game); viewflipper fliptest = (viewflipper) findviewbyid(r.id.menuflipper); //this work //fliptest.setdisplayedchild(3); while (true){ try { thread.sleep(randomtime); } catch (interruptedexception e) { e.printstacktrace(); }finally{ random timermenu = new random(); randomtime = timermenu.nextint(6) * 2000; random childmenu = new random(); randomchild = childmenu.nextint(5); fliptest.setdisplayedchild(randomchild); system.out.println("executes loop"); } } }
don't block ui thread thread.sleep()(+ infinite loop), instead use handler, example, make flips:
private viewflipper mfliptest; private handler mhandler = new handler(); private random mrand = new random(); private runnable mflip = new runnable() { @override public void run() { mfliptest.setdisplayedchild(mrand.nextint()); mhandler.postdelayed(this, mrand.nextint(6) * 2000); } } //in oncreate method mfliptest = (viewflipper) findviewbyid(r.id.menuflipper); mhandler.postdelayed(mflip, randomtime);
Comments
Post a Comment