溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

RecyclerView中怎么實(shí)現(xiàn)列表倒計(jì)時(shí)

發(fā)布時(shí)間:2021-08-09 16:37:55 來源:億速云 閱讀:342 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)RecyclerView中怎么實(shí)現(xiàn)列表倒計(jì)時(shí),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

首先看下實(shí)現(xiàn)的最終效果

如何顯示列表我相信大家都會(huì),這里我只附上和倒計(jì)時(shí)功能實(shí)現(xiàn)的adapter類。

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)存的問題,通過這行代碼,將item的hashcode作為key設(shè)入SparseArray中,這樣在cancelAllTimer方法中可以一個(gè)一個(gè)取出來進(jìn)行倒計(jì)時(shí)取消操作。

countDownMap.put(holder.clock.hashCode(),holder.countDownTimer);

接著通過下面這行代碼新建一個(gè)CountDownTimer類

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í)間。 然后通過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é)束,則通過handler的異步消息機(jī)制,將消息進(jìn)行發(fā)出,通過一整個(gè)流程,最終方法會(huì)走到handler的handleMessage方法中,如果有不熟悉這個(gè)異步流程的伙伴,可以去看我以前寫的一篇異步消息機(jī)制的文章 android異步消息機(jī)制,源碼層面徹底解析。好了,接下來就來看看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);    }   }  } };

關(guān)于RecyclerView中怎么實(shí)現(xiàn)列表倒計(jì)時(shí)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI