繁体   English   中英

ViewPager中的倒数计时器无法正常工作

[英]countdown timer in viewpager not working properly

我创建了一个简单的viewpager和片段,每个视图创建了两个textview和一个倒数计时器。 这是我的viewpager适配器。

我用在分页器中的索引调用一个函数(populateView),然后基于该索引填充内容。

我遇到的问题是,当我的cmpDetails有3个视图,并且每个视图都有自己的计时器时,在第一个视图中单击计时器会在其他视图中随机触发计时器...因此,如果我向右滑动到其他位置,即使我没有点击该特定计时器,我仍然看到计时器在倒数。 因此,例如,“视图1”计时器启动“视图3”计时器。

有人可以帮我弄清楚我所缺少的吗? 谢谢

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();
        }
    });

}

之所以发生这种情况,是因为您对所有视图都使用了一个Timer实例。 尝试对每个视图分别使用不同的实例。 总共3个实例。 比起每个视图,您将拥有1个计时器。

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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