Android如何利用RecyclerView实现列表倒计时效果实例代码

 行业动态     |      2019-07-31 09:54

前言

最近面试时,面试官问了一个列表倒计时效果如何实现,然后脑袋突然懵的了oo,现在记录一下。






经过思考,包括性能、实现等方面,决定使用第2种方式实现。



 * 总共的倒计时的时间,单位:毫秒 
 * 例: 2019-02-23 11:00:30 与 2019-02-23 11:00:00 之间的相差的毫秒数 
 private long totaltime; 
 /** 
 * 倒计时是否在暂停状态 
 private boolean ispause = true; 


mtimer.schedule;

timertask

 class mytask extends timertask {
 @override
 public void run {
 if ) {
 return;
 int size = mlist.size;
 countdowntimer凯发k8国际下载bean bean;
 long totaltime;
 for  {
 bean = mlist.get;
 if ) {//不处于暂停状态
 totaltime = bean.gettotaltime - 1000;
 if  {
 bean.setpause;
 bean.settotaltime;
 bean.settotaltime;
 message message = mhandler.obtainmessage;
 message.arg1 = i;
 mhandler.sendmessage;
 }

 mhandler = new handler) {
 @override
 public void handlemessage {
 switch  {
 case 1:
 notifyitemchanged;
 break;
 };

2.销毁资源操作:

 * 销毁资源
 public void destroy {
 mhandler.removemessages;
 if  {
 mtimer.cancel;
 mtimer.purge;
 mtimer = null;
 }

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) {
 @override
 public void handlemessage {
 switch  {
 case 1:
 notifyitemchanged;
 break;
 mtask = new mytask;
 public void bindadaptertorecyclerview {
 view.setadapter;
 * 设置新的数据源
 * @param list 数据
 public void setnewdata {
 destroy;
 mlist.clear;
 mlist.addall;
 notifydatasetchanged;
 if  {
 mtimer = new timer;
 mtimer.schedule;
 * 销毁资源
 public void destroy {
 mhandler.removemessages;
 if  {
 mtimer.cancel;
 mtimer.purge;
 mtimer = null;
 @nonnull
 @override
 public holder oncreateviewholder {
 view view = layoutinflater.from).inflate;
 return new holder;
 @override
 public void onbindviewholder {
 if ) {
 onbindviewholder;
 return;
 //更新某个控件,比如说只需要更新时间信息,其他不用动
 countdowntimerbean bean = mlist.get;
 long day = bean.gettotaltime / ;
 long hour =  /  - day * 24);
 long min =  / ) - day * 24 * 60 - hour * 60);
 long s =  / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
 holder.tvtime.settext;
 holder.btnaction.settext "开始" : "暂停");
 holder.btnaction.setenabled != 0);
 @override
 public void onbindviewholder {
 holder.ivicon.setimageresource;
 final countdowntimerbean bean = mlist.get;
 long day = bean.gettotaltime / ;
 long hour =  /  - day * 24);
 long min =  / ) - day * 24 * 60 - hour * 60);
 long s =  / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
 holder.tvtime.settext;
 holder.btnaction.settext "开始" : "暂停");
 holder.btnaction.setenabled != 0);
 holder.btnaction.setonclicklistener {
 @override
 public void onclick {
 if ) {
 bean.setpause;
 holder.btnaction.settext;
 } else {
 bean.setpause;
 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 {
 super;
 ivicon = itemview.findviewbyid;
 tvtime = itemview.findviewbyid;
 btnaction = itemview.findviewbyid;
 class mytask extends timertask {
 @override
 public void run {
 if ) {
 return;
 int size = mlist.size;
 countdowntimerbean bean;
 long totaltime;
 for  {
 bean = mlist.get;
 if ) {//不处于暂停状态
 totaltime = bean.gettotaltime - 1000;
 if  {
 bean.setpause;
 bean.settotaltime;
 bean.settotaltime;
 message message = mhandler.obtainmessage;
 message.arg1 = i;
 mhandler.sendmessage;
}

源码

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对萬仟网的支持。