如何为recyclerview

时间:2016-02-11 10:03:50

标签: android view timer countdowntimer

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

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");


}

1 个答案:

答案 0 :(得分:2)

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