利用RecyclerView实现一个非常简单的列表倒计时
一、先上效果图
二、GitHub
代码地址,欢迎指正https://github.com/MNXP/Countdown
三、思路
在adapter中设置timer倒计时,然后利用RecyclerView更新单个item特性实现
四、实现
1)变量
useTime倒计时时间
timeFlag用来判断是否有倒计时,倒计时结束更新UI
2)倒计时
遍历list,设置时间useTime-1000,然后判断时间和timeFlag更新显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| private void startTime() { new Timer().schedule(new TimerTask() { @Override public void run() { ((Activity) mContext).runOnUiThread(new Runnable() { @Override public void run() { for (int i = 0; i < mTimeDownBeanList.size(); i++) { long useTime = mTimeDownBeanList.get(i).getUseTime(); if (useTime > 1000) { useTime -= 1000; mTimeDownBeanList.get(i).setUseTime(useTime); if (useTime <= 1000 && mTimeDownBeanList.get(i).isTimeFlag()) { mTimeDownBeanList.get(i).setTimeFlag(false); CountDownAdapter.this.notifyItemChanged(i); }else { mTimeDownBeanList.get(i).setTimeFlag(true); CountDownAdapter.this.notifyItemChanged(i); }
}
}
} });
} }, 0, 1000); }
|
3)常规操作(设置倒计时时间)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| private void setTime(ViewHolder holder, int position) throws ParseException{ TimeDownBean timeDownBean = mTimeDownBeanList.get(position); if (timeDownBean.getUseTime() > 1000) { holder.timeTv.setVisibility(View.VISIBLE); long useTime = timeDownBean.getUseTime(); useTime = useTime / 1000; setTimeShow(useTime, holder);
}else { holder.timeTv.setVisibility(View.GONE); }
}
private void setTimeShow(long useTime, ViewHolder holder) { int hour = (int) (useTime / 3600 ); int min = (int) (useTime / 60 % 60); int second = (int) (useTime % 60); int day = (int) (useTime / 3600 / 24); String mDay, mHour, mMin, mSecond;//天,小时,分钟,秒 second--; if (second < 0) { min--; second = 59; if (min < 0) { min = 59; hour--; } } if (hour < 10) { mHour = "0" + hour; } else { mHour = "" + hour; } if (min < 10) { mMin = "0" + min; } else { mMin = "" + min; } if (second < 10) { mSecond = "0" + second; } else { mSecond = "" + second; } String strTime = "上架倒计时 " +mHour + ":" + mMin + ":" + mSecond + ""; holder.timeTv.setText(strTime);
}
|
4)注意
防止更新item时防止抖动
((SimpleItemAnimator) mRecycleListView.getItemAnimator()).setSupportsChangeAnimations(false);
5)Adapter完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| public class CountDownAdapter extends RecyclerView.Adapter { private Context mContext; private List<TimeDownBean> mTimeDownBeanList; public CountDownAdapter(Context context,List<TimeDownBean> mTimeDownBeanList){ this.mContext = context; this.mTimeDownBeanList = mTimeDownBeanList; startTime(); }
/** * 列表倒计时 */ private void startTime() { new Timer().schedule(new TimerTask() { @Override public void run() { ((Activity) mContext).runOnUiThread(new Runnable() { @Override public void run() { for (int i = 0; i < mTimeDownBeanList.size(); i++) { long useTime = mTimeDownBeanList.get(i).getUseTime(); if (useTime > 1000) { useTime -= 1000; mTimeDownBeanList.get(i).setUseTime(useTime); if (useTime <= 1000 && mTimeDownBeanList.get(i).isTimeFlag()) { mTimeDownBeanList.get(i).setTimeFlag(false); CountDownAdapter.this.notifyItemChanged(i); }else { mTimeDownBeanList.get(i).setTimeFlag(true); CountDownAdapter.this.notifyItemChanged(i); }
}
}
} });
} }, 0, 1000); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(mContext,R.layout.count_down_item,null); return new ViewHolder(view); }
@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof ViewHolder){ ViewHolder viewHolder = (ViewHolder) holder; TimeDownBean timeDownBean = mTimeDownBeanList.get(position); viewHolder.contentTv.setText(timeDownBean.getContent()); try { setTime(viewHolder, position); } catch (ParseException e) { e.printStackTrace(); } }
}
private void setTime(ViewHolder holder, int position) throws ParseException{ TimeDownBean timeDownBean = mTimeDownBeanList.get(position); if (timeDownBean.getUseTime() > 1000) { holder.timeTv.setVisibility(View.VISIBLE); long useTime = timeDownBean.getUseTime(); useTime = useTime / 1000; setTimeShow(useTime, holder);
}else { holder.timeTv.setVisibility(View.GONE); }
}
private void setTimeShow(long useTime, ViewHolder holder) { int hour = (int) (useTime / 3600 ); int min = (int) (useTime / 60 % 60); int second = (int) (useTime % 60); int day = (int) (useTime / 3600 / 24); String mDay, mHour, mMin, mSecond;//天,小时,分钟,秒 second--; if (second < 0) { min--; second = 59; if (min < 0) { min = 59; hour--; } } if (hour < 10) { mHour = "0" + hour; } else { mHour = "" + hour; } if (min < 10) { mMin = "0" + min; } else { mMin = "" + min; } if (second < 10) { mSecond = "0" + second; } else { mSecond = "" + second; } String strTime = "上架倒计时 " +mHour + ":" + mMin + ":" + mSecond + ""; holder.timeTv.setText(strTime);
}
@Override public int getItemCount() { return mTimeDownBeanList!=null?mTimeDownBeanList.size():0; }
private class ViewHolder extends RecyclerView.ViewHolder{ private TextView contentTv; private TextView timeTv; private ViewHolder(View itemView) { super(itemView); init(itemView); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (itemClickListener!=null){ itemClickListener.onItemClick(v,getLayoutPosition()); } } }); }
private void init(View itemView) { contentTv = (TextView)itemView.findViewById(R.id.content_tv); timeTv = (TextView)itemView.findViewById(R.id.time_tv); } }
//对外部暴漏点击事件接口 public interface OnItemClickListener{ void onItemClick(View v,int position); }
public OnItemClickListener itemClickListener;
public void setItemClickListener(OnItemClickListener itemClickListener) { this.itemClickListener = itemClickListener; } }
|
如有意见和建议,及时沟通。