【问题标题】:How to add counter timer for each list item in recyclerview如何为recyclerview中的每个列表项添加计数器计时器
【发布时间】:2016-02-11 10:03:50
【问题描述】:

我想创建一个列表,其中显示每个项目的剩余时间,请告诉我如何在回收站视图中添加它,我已经看到了列表视图的解决方案,但它在回收站视图中不起作用。

How to handle multiple countdown timers in 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");


}

【问题讨论】:

    标签: android view timer countdowntimer


    【解决方案1】:

    在初始化所有小部件和适配器后,将此代码放入具有 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);
    

    【讨论】:

    • 添加 onDestroyed 方法 if (updateFuture != null) { updateFuture.cancel(true);更新未来=空; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多