欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Android

當(dāng)前位置:主頁 > 軟件編程 > Android >

RecyclerView實現(xiàn)列表倒計時

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Android|點擊: 次

最近在做一個項目,需要用到列表倒計時功能,搗鼓半天終于弄了出來,在安卓中實現(xiàn)這個效果需要用到Countdowntimer,通過這個類的使用,不僅可以實現(xiàn)倒計時的效果,還可以完美解決在實現(xiàn)倒計時過程中的兩個bug。

1.內(nèi)存問題
2.由于recyclerview的item復(fù)用導(dǎo)致不同條目的時間錯亂

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

如何顯示列表我相信大家都會,這里我只附上和倒計時功能實現(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() {
//     時間結(jié)束后進行相應(yīng)邏輯處理
    }
   }.start();
   countDownMap.put(holder.clock.hashCode(), holder.countDownTimer);
  } else {
//   時間結(jié)束 進行相應(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()這個方法解決了內(nèi)存的問題,通過這行代碼,將item的hashcode作為key設(shè)入SparseArray中,這樣在cancelAllTimer方法中可以一個一個取出來進行倒計時取消操作。

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

接著通過下面這行代碼新建一個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() {
// 時間結(jié)束后進行相應(yīng)邏輯處理
}
}.start();

分析它的源碼

public CountDownTimer(long millisInFuture, long countDownInterval) {
  mMillisInFuture = millisInFuture;
  mCountdownInterval = countDownInterval;
 }

從中可以很清楚的看出,設(shè)置了兩個值,第一個是倒計時結(jié)束時間,第二個是刷新時間的間隔時間。
然后通過start方法進行啟動,接著看下start方法中進行的處理

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ù)褂嫊r截止時間小于等0時也就是倒計時結(jié)束時,調(diào)用了onFinish方法,若時間還未結(jié)束,則通過handler的異步消息機制,將消息進行發(fā)出,通過一整個流程,最終方法會走到handler的handleMessage方法中,如果有不熟悉這個異步流程的伙伴,可以去看我以前寫的一篇異步消息機制的文章 android異步消息機制,源碼層面徹底解析。好了,接下來就來看看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);
    }
   }
  }
 };

相信這段源碼還是很通熟易懂,首先計算出剩余時間,如果剩余時間小于刷新時間,就發(fā)送一條延時消息直到時間結(jié)束,如果剩余時間大于刷新時間就調(diào)用onTick(millisLeft)方法,這個方法在我們創(chuàng)建CountDownTimer類時就進行過重寫,在里面就可以寫我們倒計時展示的具體邏輯了。至此整個流程結(jié)束。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:Android Studio3.5開發(fā)工具(安卓開發(fā)工具)安裝步驟詳解

欄    目:Android

下一篇:Android實現(xiàn)漸變啟動頁和帶有指示器的引導(dǎo)頁

本文標題:RecyclerView實現(xiàn)列表倒計時

本文地址:http://mengdiqiu.com.cn/a1/Android/9192.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有