StartFlipping for ViewFlipper after 60seconds from last touch

心已入冬 提交于 2019-12-23 22:17:22

问题


My app contains a ViewFlipper with some images. when app starts, the ViewFlipper startflipping(). When user touch the screen ViewFlipper stopflipping(). I must do that after 60 seconds from last touch, ViewFlipper to start again flipping. My class implements onTouchListener and I have this method onTouch:

public boolean onTouch(View arg0, MotionEvent arg1) {


        switch (arg1.getAction()) {
        case MotionEvent.ACTION_DOWN: {

            downXValue = arg1.getX();
            break;
        }

        case MotionEvent.ACTION_UP: {

            currentX = arg1.getX();


            if (downXValue < currentX) {
                // Set the animation
                vf.stopFlipping();
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_in));
                // Flip!
                vf.showPrevious();
            }


            if (downXValue > currentX) {
                // Set the animation
                vf.stopFlipping();
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_in));
                // Flip!
                vf.showNext();
            }

            if (downXValue == currentX) {
                final int idImage = arg0.getId();

                vf.stopFlipping();
                System.out.println("id" + idImage);
                System.out.println("last touch "+getTimeOfLastEvent());

            }
            break;
        }
        }

        // if you return false, these actions will not be recorded
        return true;
    }

and I found this method, for finding time of the last touch :

static long timeLastEvent=0;
public long getTimeOfLastEvent() {

        long duration = System.currentTimeMillis() - timeLastEvent;
        timeLastEvent = System.currentTimeMillis();
        return duration;
    }

My question is : where should I call the getTimeOfLastEvent() ? If I put it on onTouch() I will never catch the moment when getTimeOfLastEvent==60000, right?


回答1:


What you should do is create a Handler (should be an instance variable of your Activity and should be initialized during onCreate):

Handler myHandler = new Handler();

Also you will need a Runnable that can start the flipping again (also needs to be declared within your Activity):

private Runnable flipController = new Runnable() {
  @Override
  public void run() {
    vf.startFlipping();
  }
};

Then in your onClick you just post the Runnable on the Handler but delayed by 60 seconds:

myHandler.postDelayed( flipController, 60000 );

Posting it delayed means: "Run this code in 60 seconds".



来源:https://stackoverflow.com/questions/8295113/startflipping-for-viewflipper-after-60seconds-from-last-touch

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