这篇文章将为大家详细讲解有关怎么在Android中利用RecyclerView实现一个列表倒计时效果,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联主要从事网站制作、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务天宁,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
实现过程
数据实体
/** * 总共的倒计时的时间(结束时间-开始时间),单位:毫秒 * 例: 2019-02-23 11:00:30 与 2019-02-23 11:00:00 之间的相差的毫秒数 */ private long totalTime; /** * 倒计时是否在暂停状态 */ private boolean isPause = true;
倒计时
Timer
mTimer.schedule(mTask, 0, 1000);
TimerTask
class MyTask extends TimerTask { @Override public void run() { if (mList.isEmpty()) { return; } int size = mList.size(); CountDownTimerBean bean; long totalTime; for (int i = 0; i < size; i++) { bean = mList.get(i); if (!bean.isPause()) {//不处于暂停状态 totalTime = bean.getTotalTime() - 1000; if (totalTime <= 0) { bean.setPause(true); bean.setTotalTime(0); } bean.setTotalTime(totalTime); Message message = mHandler.obtainMessage(1); message.arg1 = i; mHandler.sendMessage(message); } } } }
线程交互更新item
mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: notifyItemChanged(msg.arg1, "update-time"); break; } } };
性能优化方面
1.调用notifyItemChanged()方法后,不要更新整个item(比如说item包含图片,不需要变的),所以要重写onBindViewHolder( Holder , int , List )方法:
@Override public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull List<Object> payloads) { if (payloads.isEmpty()) { onBindViewHolder(holder, position); return; } //更新某个控件,比如说只需要更新时间信息,其他不用动 CountDownTimerBean bean = mList.get(position); long day = bean.getTotalTime() / (1000 * 60 * 60 * 24); long hour = (bean.getTotalTime() / (1000 * 60 * 60) - day * 24); long min = ((bean.getTotalTime() / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (bean.getTotalTime() / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); holder.tvTime.setText("剩余时间: " + day + "天" + hour + "小时" + min + "分" + s + "秒"); holder.btnAction.setText(bean.isPause() ? "开始" : "暂停"); holder.btnAction.setEnabled(bean.getTotalTime() != 0); }
2.销毁资源操作:
/** * 销毁资源 */ public void destroy() { mHandler.removeMessages(1); if (mTimer != null) { mTimer.cancel(); mTimer.purge(); mTimer = null; } }
RecyclerView.Adapter部分源码
public class CountDownTimerAdapter extends RecyclerView.Adapter<CountDownTimerAdapter.Holder> { private static final String TAG = "CountDownTimerAdapter->"; private List<CountDownTimerBean> mList;//数据 private Handler mHandler;//线程调度,用来更新列表 private Timer mTimer; private MyTask mTask; public CountDownTimerAdapter() { mList = new ArrayList<>(); mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: notifyItemChanged(msg.arg1, "update-time"); break; } } }; mTask = new MyTask(); } public void bindAdapterToRecyclerView(@NonNull RecyclerView view) { view.setAdapter(this); } /** * 设置新的数据源 * * @param list 数据 */ public void setNewData(@NonNull List<CountDownTimerBean> list) { destroy(); mList.clear(); mList.addAll(list); notifyDataSetChanged(); if (mTimer == null) { mTimer = new Timer(); } mTimer.schedule(mTask, 0, 1000); } /** * 销毁资源 */ public void destroy() { mHandler.removeMessages(1); if (mTimer != null) { mTimer.cancel(); mTimer.purge(); mTimer = null; } } @NonNull @Override public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_count_down_timer, viewGroup, false); return new Holder(view); } @Override public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull List<Object> payloads) { if (payloads.isEmpty()) { onBindViewHolder(holder, position); return; } //更新某个控件,比如说只需要更新时间信息,其他不用动 CountDownTimerBean bean = mList.get(position); long day = bean.getTotalTime() / (1000 * 60 * 60 * 24); long hour = (bean.getTotalTime() / (1000 * 60 * 60) - day * 24); long min = ((bean.getTotalTime() / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (bean.getTotalTime() / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); holder.tvTime.setText("剩余时间: " + day + "天" + hour + "小时" + min + "分" + s + "秒"); holder.btnAction.setText(bean.isPause() ? "开始" : "暂停"); holder.btnAction.setEnabled(bean.getTotalTime() != 0); } @Override public void onBindViewHolder(@NonNull final Holder holder, int position) { holder.ivIcon.setImageResource(R.mipmap.ic_launcher_round); final CountDownTimerBean bean = mList.get(position); long day = bean.getTotalTime() / (1000 * 60 * 60 * 24); long hour = (bean.getTotalTime() / (1000 * 60 * 60) - day * 24); long min = ((bean.getTotalTime() / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (bean.getTotalTime() / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); holder.tvTime.setText("剩余时间: " + day + "天" + hour + "小时" + min + "分" + s + "秒"); holder.btnAction.setText(bean.isPause() ? "开始" : "暂停"); holder.btnAction.setEnabled(bean.getTotalTime() != 0); holder.btnAction.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (bean.isPause()) { bean.setPause(false); holder.btnAction.setText("暂停"); } else { bean.setPause(true); holder.btnAction.setText("开始"); } } }); } @Override public int getItemCount() { return mList.size(); } class Holder extends RecyclerView.ViewHolder { private ImageView ivIcon; private TextView tvTime; private Button btnAction; Holder(@NonNull View itemView) { super(itemView); ivIcon = itemView.findViewById(R.id.iv_icon); tvTime = itemView.findViewById(R.id.tv_time); btnAction = itemView.findViewById(R.id.btn_action); } } class MyTask extends TimerTask { @Override public void run() { if (mList.isEmpty()) { return; } int size = mList.size(); CountDownTimerBean bean; long totalTime; for (int i = 0; i < size; i++) { bean = mList.get(i); if (!bean.isPause()) {//不处于暂停状态 totalTime = bean.getTotalTime() - 1000; if (totalTime <= 0) { bean.setPause(true); bean.setTotalTime(0); } bean.setTotalTime(totalTime); Message message = mHandler.obtainMessage(1); message.arg1 = i; mHandler.sendMessage(message); } } } } }
关于怎么在Android中利用RecyclerView实现一个列表倒计时效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网页名称:怎么在Android中利用RecyclerView实现一个列表倒计时效果
本文URL:https://www.cdcxhl.com/article22/gppgjc.html
成都网站建设公司_创新互联,为您提供网站设计公司、静态网站、外贸建站、自适应网站、品牌网站设计、用户体验
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联