欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

五華ssl適用于網站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

首先看下實現(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() {//     時間結束后進行相應邏輯處理    }   }.start();   countDownMap.put(holder.clock.hashCode(), holder.countDownTimer);  } else {//   時間結束 進行相應邏輯處理  } } @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()這個方法解決了內存的問題,通過這行代碼,將item的hashcode作為key設入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() {// 時間結束后進行相應邏輯處理}}.start();

分析它的源碼

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

從中可以很清楚的看出,設置了兩個值,第一個是倒計時結束時間,第二個是刷新時間的間隔時間。 然后通過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; }

源碼中,當?shù)褂嫊r截止時間小于等0時也就是倒計時結束時,調用了onFinish方法,若時間還未結束,則通過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);    }   }  } };

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

文章標題:RecyclerView中怎么實現(xiàn)列表倒計時
網址分享:http://chinadenli.net/article38/pgphsp.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供商城網站、外貿建站、小程序開發(fā)ChatGPT、外貿網站建設、軟件開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網站建設網站維護公司