首先,新年的一年里祝大家,心想事成,雞年大吉。去年的時(shí)候,我們做時(shí)間控件的時(shí)候一直遺留一個(gè)問題那就是正計(jì)時(shí)控件一直沒有好的解決方案,我們很想把CountDownView既支持正計(jì)時(shí)又能支持倒計(jì)時(shí)。基于這個(gè)想法,便有了今天這篇文章,原理不在介紹,其實(shí)很簡(jiǎn)單,主要是我們知道怎么用,此控件的優(yōu)點(diǎn)有:

創(chuàng)新互聯(lián)建站提供高防物理服務(wù)器租用、云服務(wù)器、香港服務(wù)器、服務(wù)器托管德陽等
* 實(shí)現(xiàn)了正計(jì)時(shí)倒計(jì)時(shí)的統(tǒng)一
* 優(yōu)化了Adapter,不再綁定控件Id
* 一個(gè)屬性實(shí)現(xiàn)正倒計(jì)時(shí)
* 不在為具體的時(shí)間屬性設(shè)置別名
具體用法
1、xml文件
屬性 app:isCountUp=”false”代表倒計(jì)時(shí) true為正計(jì)時(shí)
<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、實(shí)體類
要繼承TimeEntity
eg:
package com.delta.counttimeview;
/**
* @description :正計(jì)時(shí)的
* @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如下
倒計(jì)時(shí)主要是==要注意在數(shù)據(jù)源的時(shí)候初始化endTime屬性,也就是截止時(shí)間還有相應(yīng)的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();
}
}
}
正計(jì)時(shí)==主要是在獲得數(shù)據(jù)源的時(shí)候初始化createTime,也就是起始時(shí)間和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();
}
}
}
下載源碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章名稱:Android基于CountDownView的時(shí)間控件擴(kuò)展
文章分享:http://www.yijiale78.com/article36/ihojpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站制作、全網(wǎng)營(yíng)銷推廣、網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)