ViewPager - 如何开始在每一页上不同的定时器? [英] ViewPager — how to start different timers on each page?

查看:156
本文介绍了ViewPager - 如何开始在每一页上不同的定时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我想要的是在ViewPager的每个页面创建新的独立的定时器。
我目前的实现包含所有页面的一个处理程序。我开始使用ViewPager.OnPageChangeListener.onPageSelected()新TimerTask的,但我可以我的应用程序的工作过程中看到,可运行堆栈和定时器的推移越来​​越快,一次更新所有textviews,尽管我试图清除所有的回调中该处理程序。

应该是如何工作的:轻扫至1 - 定时器1开始,刷卡第2页 - 定时器1复位并停止,定时器2开始等等

  viewPager.setOnPageChangeListener(新OnPageChangeListener(){        公共无效onPageScrollStateChanged(INT页){        }        公共无效onPageScrolled(INT位置,浮ARG1,ARG2 INT){
        }        公共无效使用onPageSelected(INT位置){
            查看当前页=((QuestionPagerAdapter)viewPager.getAdapter())获得(位置)。            timerText =(TextView中)currentPage.findViewById(R.id.timerText);
            handler.removeCallbacks(doTick);
            如果(timerText!= NULL)startTimer所();
        }    });

Runnable接口的实现:

 最后的Runnable doTick =新的Runnable(){    公共无效的run(){
        秒 - ;
        如果(秒== -1){
            minutes--;
            秒= 59;
        }
        字符串secText =将String.valueOf(秒);
        如果(秒-1,10)secText =0+ secText;
        timerText.setText(0+将String.valueOf(分钟)+:+ secText);
        viewPager.getAdapter()notifyDataSetChanged()。
    }};私人无效startTimer所(){
    的for(int i = 1; I< =秒;我++){
        TimerTask的任务=新的TimerTask(){
            @覆盖
            公共无效的run(){
                handler.post(doTick);
            }
        };
        timer.schedule(任务,我* 1000);
    }
}


解决方案

你在哪里实例化新doTick Runnable接口?这应该是只进行一次,否则你将无法去除关联到doTick实例的所有回调,你会失去它,并用新的实例取而代之。

声明它有一个数据字段和修改你startTimer所的方法如下:

 私人无效startTimer所(TextView的的TextView){
    如果(doTicks!= NULL){
       handler.removeCallbacks(doTick);
       doTicks =新doTicks(的TextView);
    }
    的for(int i = 1; I< =秒;我++){
        TimerTask的任务=新的TimerTask(){
            @覆盖
                公共无效的run(){
                    handler.post(doTick);
                }
            };
        timer.schedule(任务,我* 1000);
    }
}

您还需要提供一个参数,你doTicks的Runnable给它有更新字段的引用。通过这个参数startTimer所为好,不要让这个字段作为活动的数据成员的引用,只能作为doTicks(所以你需要创建一个真正的静态内部类)。

的数据成员

All I want is to create new separate timer for each page in the ViewPager. My current realization contains the one handler for all of the pages. I start new TimerTask using ViewPager.OnPageChangeListener.onPageSelected(), but as I can see during my application working, runnables stacks and timer goes faster and faster, updating all textviews at one time, even though I'm trying to remove all callbacks in the handler.

How it should work: swipe to page 1 — timer 1 starts, swipe to page 2 — timer 1 resets and stops, timer 2 starts and so on.

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

        public void onPageScrollStateChanged(int page) {

        }

        public void onPageScrolled(int position, float arg1, int arg2) {
        }

        public void onPageSelected(int position) {
            View currentPage = ((QuestionPagerAdapter) viewPager.getAdapter()).get(position);

            timerText = (TextView) currentPage.findViewById(R.id.timerText);
            handler.removeCallbacks(doTick);
            if (timerText != null) startTimer();
        }

    });

Runnable realization:

final Runnable doTick = new Runnable() {

    public void run() {
        seconds--;
        if (seconds == -1) {
            minutes--;
            seconds = 59;
        }
        String secText = String.valueOf(seconds);
        if (seconds < 10) secText = "0" + secText;
        timerText.setText("0" + String.valueOf(minutes) + ":" + secText);
        viewPager.getAdapter().notifyDataSetChanged();
    }

};

private void startTimer() {
    for (int i = 1; i <= seconds; i++) {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                handler.post(doTick);
            }
        };
        timer.schedule(task, i * 1000); 
    }
}

解决方案

Where do you instanciate new doTick Runnable ? This should be done only once, otherwise you will not be able to remove all callbacks associated to a doTick instance as you would have lost it and replaced it with a new instance.

Declare it has a data field and modify you startTimer method as follow :

private void startTimer( TextView textView ) {
    if( doTicks != null  ) {
       handler.removeCallbacks(doTick);
       doTicks = new doTicks( textView );
    }
    for (int i = 1; i <= seconds; i++) {
        TimerTask task = new TimerTask() {
            @Override
                public void run() {
                    handler.post(doTick);
                }
            };
        timer.schedule(task, i * 1000); 
    }
}

You will also need to provide a parameter to your doTicks Runnable to give it a reference to the field it has to update. Pass this parameter to startTimer as well, and don't keep a reference on this field as data member of the activity, only as a data member of doTicks (so you need to create a true static inner class).

这篇关于ViewPager - 如何开始在每一页上不同的定时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆