在选项卡式活动中更改片段时,片段中运行的双计时器

时间:2018-07-03 09:30:14

标签: android android-fragments timer

我已经在android中实现了选项卡式活动。它包含3个片段中的3个选项卡。在一个片段上,我设置了一个倒数计时器。

我的问题是:在更改选项卡式活动中的那些片段并通过选项卡使用计时器返回片段时,我的旧计时器值继续,但是新的计时器值实例也在运行,即显示第一个设置的计时器值,然后翻转到新的设置的计时器值,然后翻转到第一个设置的时间,依此类推。

选项卡式活动与android studio提供的内容相同,但打开片段的改动很小:

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position){
            case 0:
                fragment = new FragmentA();
                break;
            case 1:
                fragment = new FragmentB();
                break;
            case 2:
                fragment = new FragmentC();
                break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}

我在FragmentA中实现的倒数计时器为:

public class FragmentA extends Fragment {
TextView mCountDown;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_A, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mCountDown = view.findViewById(R.id.tv1);

    new CountDownTimer(100000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            long check = millisUntilFinished;
            int hour = (int) check / (1000 * 60 * 60);
            check = (int) (check - (hour * 1000 * 60 * 60));
            int min = (int) check / (1000 * 60);
            check = (int) (check - (min * 1000 * 60));
            int sec = (int) check / (1000);

            mCountDown.setText(hour + " : " + min + " : " + sec);
        }

        @Override
        public void onFinish() {
            mCountDown.setText("Reached");
        }
    }.start();

}
}

如果有人能帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:0)

好的,问题出在FragmentA本身。我已经在创建问题的 onViewCreated()方法上设置了countdowntimer millisec值。

因此解决方案是在onCreate()方法上初始化millisec值,并使用该值在onViewCreated()方法上启动countdowntimer。并且还使用了onResume()方法来继续相同的计时器,尽管通过选项卡更改了片段。 (为避免重复代码,还创建了一个小的计时器方法。)

FragmentA上的代码:

public class FragmentA extends Fragment {
private long millisecvalue;
TextView mCountDown;
CountDownTimer countDownTimer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    millisecvalue = 1000000;        //initial timer value
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_A, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    mCountDown = view.findViewById(R.id.tv1);

    startContinueTimer();

}

private void startContinueTimer() {
    countDownTimer =  new CountDownTimer(millisecvalue, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            long check = millisUntilFinished;
            int hour = (int) check / (1000 * 60 * 60);
            check = (int) (check - (hour * 1000 * 60 * 60));
            int min = (int) check / (1000 * 60);
            check = (int) (check - (min * 1000 * 60));
            int sec = (int) check / (1000);

            mCountDown.setText(hour + " : " + min + " : " + sec);
            millisecvalue = millisUntilFinished;    //update milisecvalue "VERY IMPORTANT"
        }

        @Override
        public void onFinish() {
            mCountDown.setText("Reached");
        }
    }.start();
}

@Override
public void onResume() {
    super.onResume();
    startContinueTimer();
}
}

希望这会有所帮助。如果有更好的方法,请告诉我!