简体   繁体   中英

countdown timer in viewpager not working properly

I have created a simple viewpager and fragment that creates a couple of textviews and a countdown timer, per view. Here is my adapter for the viewpager.

I call a function (populateView) with my index in the pager and based on that I populate the contents.

The problem I am running into is that, when my cmpDetails has say 3 views, and each with their own timer, clicking the timer in the first view randomly sets off timer in some other view...so if I swipe right to other positions, I see the timer counting down even though I did not click on THAT specific timer. So for example, View 1 timer starts view 3 timer.

Can someone help me figure out what I am missing, please? Thanks

public class CustomPagerAdapter extends PagerAdapter {
    private ArrayList<HashMap<String, String>> cmpDetails;
    TextView timer;
    TextView nextName;
    TextView eName;
    TextView cmpTitle;
    CountDownTimer ct;
    boolean coutndownIsRunning = false;
    private ViewGroup groupLayout;


        private Context mContext;
        public CustomPagerAdapter(Context context, ArrayList<HashMap<String, String>> cmpDetails) {
    this.cmpDetails=cmpDetails;
    mContext = context;
}

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.cmp_child_item, collection, false);

    cmpTitle = (TextView) layout.findViewById(R.id.cmp_title);
    eName = (TextView) layout.findViewById(R.id.ename);
    nextName = (TextView) layout.findViewById(R.id.next);
    timer = (TextView) layout.findViewById(R.id.timer);

    textView6 = (TextView) layout.findViewById(R.id.textView6);
    textView7 = (TextView) layout.findViewById(R.id.textView7);
    secs = (TextView) layout.findViewById(R.id.secs);

    if (ct != null)  {
        ct.cancel();
    }

    groupLayout = layout;
    PopulateView(position);
    collection.addView(layout);

    return layout;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
    collection.removeView((View) view);
    if (ct != null)  {
        ct.cancel();
    }

}

@Override
public int getCount() {
    return cmpDetails.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public CharSequence getPageTitle(int position) {
    return cmpDetails.get(position).get("name");
}

public void PopulateView(final int index) {
    timer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ct != null && coutndownIsRunning)  {
                ct.cancel();
            }
            ct = new CountDownTimer(Integer.parseInt(cmpDetails.get(index).get("time"))*1000, 1000) {

                public void onTick(long millisUntilFinished) {
                    coutndownIsRunning = true;
                    timer.setText("" + millisUntilFinished / 1000);
                }

                public void onFinish() {
                    coutndownIsRunning = false;
                    timer.setOnClickListener(null);
                    ct.cancel();
                    timer.setText("Done!");
                    if (Integer.parseInt(cmpDetails.get(index).get("time")) > 0) {
                    }
                }
            };
            ct.start();
        }
    });

}

This is happening because you are using a single instance of Timer for all views. Try to use different instances, for each view separately. In total 3 instances. Than you'll have 1 timer for each view.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM