ViewFlipper: flipping at random time intervals using random children

纵然是瞬间 提交于 2019-12-13 00:00:01

问题


I want to create a menu which "flips" at random time intervals between 5 children of a viewflipper in random order.

I tried the following code and I can get the System.out.println to display my debugging message to be logged in the logcat at random time intervals so that's working. However, my screen in the emulator is all black.

When I simply use the setDisplayedChild method in the "onCreate" method with a fixed int, it works fine. Can you help me with this? Thanks 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 would 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 the finally loop");
        }
    }

}

回答1:


Don't block the UI thread like you do with Thread.sleep()(+ infinite loop), instead use a Handler, for example, to make your 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 the onCreate method
mFliptest = (ViewFlipper) findViewById(R.id.menuFlipper);
mHandler.postDelayed(mFlip, randomTime);



回答2:


Here is my final code. I changed a few things: I put the randomTime int in the run method so that it's updated at each run and therefore the handler gets delayed randomly. Otherwise, it will be delayed according to the first run of the randomly generated number and the time intervals will stay the same. I also had to manipulate the generated randomTime int because if the randomly generated int is 0, then the delay is 0 as well and the flip happens instantly which is not what I wanted.

Thanks a million for your help! :-)

private ViewFlipper fliptest;
private Handler testHandler = new Handler();
private Random mRand = new Random();
private Random timerMenu = new Random();
int randomTime;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);


    fliptest = (ViewFlipper) findViewById(R.id.menuFlipper);
    testHandler.postDelayed(mFlip, randomTime);
}

private Runnable mFlip = new Runnable() {

    @Override
    public void run() {
        randomTime  = (timerMenu.nextInt(6) + 1) * 2000;

        System.out.println("executes the run method " + randomTime);
        fliptest.setDisplayedChild(mRand.nextInt(6));
        testHandler.postDelayed(this, (mRand.nextInt(6)+ 1) * 2000);
    }    
};


来源:https://stackoverflow.com/questions/17726228/viewflipper-flipping-at-random-time-intervals-using-random-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!