倒计时控件Countdowntimer和多布局适配器BaseMultiItemQuickAdapter如何实现列表倒计时

Perfect丶稀饭 2018-09-29 02:43:56
用viewpager+fragment+recyclerview设置类似淘宝我的订单页布局;
使用的adapter继承BaseMultiItemQuickAdapter;
其中一个fragment要给每个item设置一个倒计时,问题来了:
我是使用Countdowntimer这个倒计时控件,如果仅设置一个Countdowntimer的话,在onTick方法里helper.setText只能处理最后一个item的倒计时;
根据网上提供的普通Baseadapter是可以给每个ViewHolder设置一个countdowntimer控件在去操作,但在BaseMultiItemQuickAdapter的方法的
@Override
protected void convert(final BaseViewHolder helper, final ListBean item)

不知道该怎么给每一个helper设置控件countdowntimer,实现最后一个有倒计时方法的代码
if (mTimer != null) {
mTimer.cancel(); //防止new出多个导致时间跳动加速
mTimer = null;
}
mTimer = new CountDownTimer(mCloseTime - DateUtils.getCurDateStr(), 1000) {
@Override
public void onTick(long millisUntilFinished) {
helper.setText(R.id.tv_order_status, HcBaseApplication.hcString(R.string.shop_order_close_time, DateUtils.getmmss(millisUntilFinished / 1000)));
}

@Override
public void onFinish() {
helper.setText(R.id.tv_order_status, "交易关闭");
}
};
mTimer.start();

有没有大佬能指点一下,不胜感激!

...全文
1196 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jklwan 2018-10-09
  • 打赏
  • 举报
回复
既然是列表,当然是一个item对应一个倒计时。 自定义holder存储CountDownTimer,在convert中进行处理,写的Adapter demo

public class CountDownAdapter extends BaseQuickAdapter<Long, CountDownAdapter.CountDownHolder> {

    public CountDownAdapter() {
        super(R.layout.item_countdown);
    }

    @Override
    protected void convert(CountDownHolder helper, Long item) {
        if (helper.timer != null) {
            helper.timer.cancel();
        }
        long left = item - System.currentTimeMillis();
        if (left <= 0) {
            helper.timer = null;
            helper.setText(R.id.time, "倒计时结束");
        } else {
            helper.setText(R.id.time, String.format(Locale.getDefault(), "%1$ss", (left / 1000 + (left % 1000 == 0 ? 0 : 1))));
            helper.timer = new CustomCountDown(left, 1000, helper.getView(R.id.time));
            helper.timer.start();
        }
    }

    private class CustomCountDown extends CountDownTimer {
        private TextView time;

        public CustomCountDown(long millisInFuture, long countDownInterval, TextView time) {
            super(millisInFuture, countDownInterval);
            this.time = time;
        }

        @Override
        public void onTick(long left) {
            time.setText(String.format(Locale.getDefault(), "%1$ss", (left / 1000 + (left % 1000 == 0 ? 0 : 1))));
        }

        @Override
        public void onFinish() {
            time.setText("倒计时结束");
        }
    }

    public class CountDownHolder extends BaseViewHolder {
        private CountDownTimer timer;

        public CountDownHolder(View view) {
            super(view);
        }
    }
}

80,493

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧