Android基于CountDownView的时间控件扩展
程序员文章站
2023-12-19 09:26:28
首先,新年的一年里祝大家,心想事成,鸡年大吉。去年的时候,我们做时间控件的时候一直遗留一个问题那就是正计时控件一直没有好的解决方案,我们很想把countdownview既支...
首先,新年的一年里祝大家,心想事成,鸡年大吉。去年的时候,我们做时间控件的时候一直遗留一个问题那就是正计时控件一直没有好的解决方案,我们很想把countdownview既支持正计时又能支持倒计时。基于这个想法,便有了今天这篇文章,原理不在介绍,其实很简单,主要是我们知道怎么用,此控件的优点有:
* 实现了正计时倒计时的统一
* 优化了adapter,不再绑定控件id
* 一个属性实现正倒计时
* 不在为具体的时间属性设置别名
具体用法
1、xml文件
属性 app:iscountup=”false”代表倒计时 true为正计时
<com.delta.library.counttimeview android:id="@+id/cv_countview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_gravity="center_horizontal" android:layout_margintop="20dp" app:iscountup="false" app:isshowday="true" app:isshowhour="true" app:isshowmillisecond="false" app:isshowminute="true" app:isshowsecond="true" app:suffixday="天" app:suffixdayrightmargin="10dp" app:suffixgravity="center" app:suffixhour=":" app:suffixminute=":" app:suffixtextcolor="#e61010" app:suffixtextsize="17sp" app:timetextcolor="#e60b0b" app:timetextsize="20sp" />
2、实体类
要继承timeentity
eg:
package com.delta.counttimeview; /** * @description :正计时的 * @author : v.wenju.tian * @date : 2017/2/6 15:31 */ public class itementity extends timeentity { private string title; private long time; public itementity() { } public itementity(int id, long endtime, long createtime, string title, long uptime) { super(id, endtime, createtime); this.title = title; this.time = uptime; } public long gettime() { return time; } public void settime(long time) { this.time = time; } public void settitle(string title) { this.title = title; } public string gettitle() { return title; } }
3、adapter如下
倒计时 主要是==要注意在数据源的时候初始化endtime属性,也就是截止时间还有相应的entityid==
public class countdownactivity extends appcompatactivity { private recyclerview rv; private list<itementity> datas = new arraylist<>(); private itemcountviewadapter<itementity> mmyadapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_count_down); rv = ((recyclerview) findviewbyid(r.id.rv)); long time =system.currenttimemillis(); for (int i = 0; i < 100; i++) { itementity entity = new itementity(); entity.setentityid(i); entity.settitle("第" + i); entity.settime(i *60 *1000l); entity.setendtime(time + entity.gettime()); datas.add(entity); } mmyadapter = new itemcountviewadapter<itementity>(this, datas) { @override protected int getcountviewid() { return r.id.cv_countview; } @override protected int getlayoutid() { return r.layout.item_count_down; } @override protected void convert(itemtimeviewholder holder, itementity itementity, int position) { holder.settext(r.id.tv_title, itementity.gettitle()); } }; rv.setlayoutmanager(new linearlayoutmanager(this)); rv.setadapter(mmyadapter); } @override protected void onresume() { super.onresume(); if (null != mmyadapter) { mmyadapter.startrefreshtime(); } } @override protected void onpause() { super.onpause(); if (null != mmyadapter) { mmyadapter.cancelrefreshtime(); } } @override public void ondestroy() { super.ondestroy(); if (null != mmyadapter) { mmyadapter.cancelrefreshtime(); } } }
正计时 ==主要是在获得数据源的时候初始化createtime,也就是起始时间和entityid==
public class countupactivity extends appcompatactivity { private recyclerview rv; private list<itementity> datas = new arraylist<>(); private itemcountviewadapter<itementity> mmyadapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_count_up); rv = ((recyclerview) findviewbyid(r.id.rv)); for (int i = 0; i < 100; i++) { itementity entity = new itementity(); entity.setentityid(i); entity.settitle("第" + i); entity.settime(i * 60 * 60 * 1000l); entity.setcreatetime(system.currenttimemillis() - entity.gettime()); datas.add(entity); } mmyadapter = new itemcountviewadapter<itementity>(this, datas) { @override protected int getcountviewid() { return r.id.cv_countview; } @override protected int getlayoutid() { return r.layout.list_item; } @override protected void convert(itemtimeviewholder holder, itementity itementity, int position) { holder.settext(r.id.tv_title, itementity.gettitle()); } }; rv.setlayoutmanager(new linearlayoutmanager(this)); rv.setadapter(mmyadapter); } @override protected void onresume() { super.onresume(); if (null != mmyadapter) { mmyadapter.startrefreshtime(); } } @override protected void onpause() { super.onpause(); if (null != mmyadapter) { mmyadapter.cancelrefreshtime(); } } @override public void ondestroy() { super.ondestroy(); if (null != mmyadapter) { mmyadapter.cancelrefreshtime(); } } }
下载源码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。