RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)
最近在做一個(gè)項(xiàng)目,需要用到列表倒計(jì)時(shí)功能,搗鼓半天終于弄了出來(lái),在安卓中實(shí)現(xiàn)這個(gè)效果需要用到Countdowntimer,通過(guò)這個(gè)類(lèi)的使用,不僅可以實(shí)現(xiàn)倒計(jì)時(shí)的效果,還可以完美解決在實(shí)現(xiàn)倒計(jì)時(shí)過(guò)程中的兩個(gè)bug。
1.內(nèi)存問(wèn)題
2.由于recyclerview的item復(fù)用導(dǎo)致不同條目的時(shí)間錯(cuò)亂
首先看下實(shí)現(xiàn)的最終效果
如何顯示列表我相信大家都會(huì),這里我只附上和倒計(jì)時(shí)功能實(shí)現(xiàn)的adapter類(lèi)。
public class ClockAdapter extends RecyclerView.Adapter<ClockAdapter.ClockViewHolder> {
private SparseArray<CountDownTimer> countDownMap = new SparseArray<>();
@Override
public ClockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv, parent, false);
return new ClockViewHolder(view);
}
/**
* 清空資源
*/
public void cancelAllTimers() {
if (countDownMap == null) {
return;
}
for (int i = 0,length = countDownMap.size(); i < length; i++) {
CountDownTimer cdt = countDownMap.get(countDownMap.keyAt(i));
if (cdt != null) {
cdt.cancel();
}
}
}
@Override
public void onBindViewHolder(final ClockViewHolder holder, int position) {
long betweenDate;
if (position == 0) {
betweenDate= DateUtil.getLeftTime("2017-8-8 12:10:10");
} else {
betweenDate= DateUtil.getLeftTime("2017-8-9 15:10:10");
}
if (holder.countDownTimer != null) {
holder.countDownTimer.cancel();
}
if (betweenDate > 0) {
holder.countDownTimer = new CountDownTimer(betweenDate, 1000) {
public void onTick(long millisUntilFinished) {
millisUntilFinished = millisUntilFinished / 1000;
int hours = (int) (millisUntilFinished / (60 * 60));
int leftSeconds = (int) (millisUntilFinished % (60 * 60));
int minutes = leftSeconds / 60;
int seconds = leftSeconds % 60;
final StringBuffer sBuffer = new StringBuffer();
sBuffer.append(addZeroPrefix(hours));
sBuffer.append(":");
sBuffer.append(addZeroPrefix(minutes));
sBuffer.append(":");
sBuffer.append(addZeroPrefix(seconds));
holder.clock.setText(sBuffer.toString());
}
public void onFinish() {
// 時(shí)間結(jié)束后進(jìn)行相應(yīng)邏輯處理
}
}.start();
countDownMap.put(holder.clock.hashCode(), holder.countDownTimer);
} else {
// 時(shí)間結(jié)束 進(jìn)行相應(yīng)邏輯處理
}
}
@Override
public int getItemCount() {
return 25;
}
class ClockViewHolder extends RecyclerView.ViewHolder {
TextView clock;
CountDownTimer countDownTimer;
public ClockViewHolder(View itemView) {
super(itemView);
clock = (TextView) itemView.findViewById(R.id.clock);
}
}
}
其中cancelAllTimer()這個(gè)方法解決了內(nèi)存的問(wèn)題,通過(guò)這行代碼,將item的hashcode作為key設(shè)入SparseArray中,這樣在cancelAllTimer方法中可以一個(gè)一個(gè)取出來(lái)進(jìn)行倒計(jì)時(shí)取消操作。
countDownMap.put(holder.clock.hashCode(),holder.countDownTimer);
接著通過(guò)下面這行代碼新建一個(gè)CountDownTimer類(lèi)
holder.countDownTimer = new CountDownTimer(betweenDate, 1000) {
public void onTick(long millisUntilFinished) {
millisUntilFinished = millisUntilFinished / 1000;
int hours = (int) (millisUntilFinished / (60 * 60));
int leftSeconds = (int) (millisUntilFinished % (60 * 60));
int minutes = leftSeconds / 60;
int seconds = leftSeconds % 60;
final StringBuffer sBuffer = new StringBuffer();
sBuffer.append(addZeroPrefix(hours));
sBuffer.append(":") sBuffer.append(addZeroPrefix(minutes));
sBuffer.append(":");
sBuffer.append(addZeroPrefix(seconds));
holder.clock.setText(sBuffer.toString());
}
public void onFinish() {
// 時(shí)間結(jié)束后進(jìn)行相應(yīng)邏輯處理
}
}.start();
分析它的源碼
public CountDownTimer(long millisInFuture, long countDownInterval) {
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
}
從中可以很清楚的看出,設(shè)置了兩個(gè)值,第一個(gè)是倒計(jì)時(shí)結(jié)束時(shí)間,第二個(gè)是刷新時(shí)間的間隔時(shí)間。
然后通過(guò)start方法進(jìn)行啟動(dòng),接著看下start方法中進(jìn)行的處理
public synchronized final CountDownTimer start() {
mCancelled = false;
if (mMillisInFuture <= 0) {
onFinish();
return this;
}
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
}
源碼中,當(dāng)?shù)褂?jì)時(shí)截止時(shí)間小于等0時(shí)也就是倒計(jì)時(shí)結(jié)束時(shí),調(diào)用了onFinish方法,若時(shí)間還未結(jié)束,則通過(guò)handler的異步消息機(jī)制,將消息進(jìn)行發(fā)出,通過(guò)一整個(gè)流程,最終方法會(huì)走到handler的handleMessage方法中,如果有不熟悉這個(gè)異步流程的伙伴,可以去看我以前寫(xiě)的一篇異步消息機(jī)制的文章 android異步消息機(jī)制,源碼層面徹底解析。好了,接下來(lái)就來(lái)看看handler的handleMessage方法。
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (CountDownTimer.this) {
if (mCancelled) {
return;
}
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
if (millisLeft <= 0) {
onFinish();
} else if (millisLeft < mCountdownInterval) {
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart=SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user's onTick taking time to execute
long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();
// special case: user's onTick took more than interval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
相信這段源碼還是很通熟易懂,首先計(jì)算出剩余時(shí)間,如果剩余時(shí)間小于刷新時(shí)間,就發(fā)送一條延時(shí)消息直到時(shí)間結(jié)束,如果剩余時(shí)間大于刷新時(shí)間就調(diào)用onTick(millisLeft)方法,這個(gè)方法在我們創(chuàng)建CountDownTimer類(lèi)時(shí)就進(jìn)行過(guò)重寫(xiě),在里面就可以寫(xiě)我們倒計(jì)時(shí)展示的具體邏輯了。至此整個(gè)流程結(jié)束。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android Studio3.5開(kāi)發(fā)工具(安卓開(kāi)發(fā)工具)安裝步驟詳解
欄 目:Android
下一篇:Android實(shí)現(xiàn)漸變啟動(dòng)頁(yè)和帶有指示器的引導(dǎo)頁(yè)
本文標(biāo)題:RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)
本文地址:http://www.jygsgssxh.com/a1/Android/9192.html
您可能感興趣的文章
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局
- 01-10Android實(shí)現(xiàn)底部導(dǎo)航欄的主界面


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文


