简体   繁体   English

如何为recyclerview中的每个列表项添加计数器计时器

[英]How to add counter timer for each list item in recyclerview

I want to create list which have items displayed remaining time for each item, please tell me how can I add this in recycler view ,I have seen solution for List view but it is not working in recycler view. 我想创建一个列表,其中显示每个项目的剩余时间,请告诉我如何在回收器视图中添加此列表,我已经看到了列表视图的解决方案,但在回收器视图中不起作用。

How to handle multiple countdown timers in ListView? 如何在ListView中处理多个倒数计时器?

  @Override
    public void onBindViewHolder(final ItemHolder holder, final int position) {

        setDefferinceTimer(holder, mListItems.get(position).expirationTime);
  /*  new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            new CountDownTimer(20000, 1000) {

                public void onTick(long millisUntilFinished) {
                    startCountDown(itemHolder, currentItem.getEnd_date() + " 00:00:00");
                    notifyDataSetChanged();
                }

                public void onFinish() {
                    //counterTextView.setText("done!");
                }
            }.start();
        }
    });
       */
    }

public void setDefferinceTimer(final RecyclerView.ViewHolder holder, long itemEndDate) {

    final ItemHolder itemHolder = (ItemHolder) holder;

    current_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    // reachableDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(itemEndDate);

    /*try {
        d1 = format.parse(current_date);
        d2 = format.parse(itemEndDate + " 00:00:00");
    } catch (ParseException e) {
        e.printStackTrace();
    }*/

    diff = itemEndDate - System.currentTimeMillis();

    diffSeconds = diff / 1000 % 60;
    diffMinutes = diff / (60 * 1000) % 60;
    diffHours = diff / (60 * 60 * 1000) % 24;
    diffDays = diff / (24 * 60 * 60 * 1000);

    itemHolder.textView_time.setText("" + diffDays + " Days " + diffHours + " Hours " + diffMinutes + " minuts " + diffSeconds + " seconds");


}

put this code in onCreateView method of your fragment having recylerview after initializing all widget and adapter 初始化所有小部件和适配器后,将此代码放入具有recylerview的片段的onCreateView方法中

final Handler mainHandler = new Handler(Looper.getMainLooper());
            updateFuture = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
                @Override## Heading ##
                public void run() {
                    final int firstVisible = linearLayoutManager.findFirstVisibleItemPosition();
                    final int lastVisible = linearLayoutManager.findLastVisibleItemPosition();

                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyItemRangeChanged(firstVisible, lastVisible - firstVisible + 1);
                        }
                    });
                }
            }, 0, 1000, TimeUnit.MILLISECONDS);

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

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