使用背景:在电商项目中的很多item的倒计时显示。
产生问题的原因:1、复用 2、代码多次调用
解决方案:使用Timer + View集合 (使用集合将View保存起来)
private Timer mTimer;
private Set<RecyclerViewViewHolder> mHolders;
public RecyclerViewAdapter(Activity activity, List<Long> itemList) {
if (activity == null || itemList == null) {
throw new IllegalArgumentException("params can't be null");
}
this.activity = activity;
this.itemList = itemList;
mHolders = new HashSet<>();
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
for (RecyclerViewViewHolder holder : mHolders) {
updateTime(holder, holder.getTime());
}
}
}, 0, 1000);
}
@Override
public void onBindViewHolder(RecyclerViewViewHolder holder, int position) {
holder.setTime(itemList.get(position));
mHolders.add(holder);
holder.mTvNum.setText(String.valueOf(position + 1));
updateTime(holder, itemList.get(position));
}
全部代码链接: https://github.com/nesger/RecyclerView/tree/feature/refresh_3
来源:https://blog.csdn.net/sinat_34153600/article/details/102722908