我有一个Recyclerview
,我需要在每一行上显示倒计时。
这里有一个类似的问题coutndown timers in listview它有一个很好的解决方案,但我需要回收视图
编辑:
以下是我的代码适配器MyAdapter
:
public class AdapterItems extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<TopCompetitions> mListItems = new ArrayList<>();
private ImageLoader mImageLoader;
private Context context;
private Handler handler;
/******************************************/
String current_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
long diff;
long diffSeconds;
long diffMinutes;
long diffHours;
long diffDays;
String reachableDate = "";
/******************************************/
private ScheduledFuture updateFuture;
public AdapterItems(Context context) {
this.context = context;
mImageLoader = AppController.getInstance().getImageLoader();
}
public void setmListItems(ArrayList<TopCompetitions> mListItems) {
this.mListItems = mListItems;
//update the adapter to reflect the new set of mListItems
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.custom_horizontal_row, parent, false);
return new ItemHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final TopCompetitions currentItem = mListItems.get(position);
final ItemHolder itemHolder = (ItemHolder) holder;
/* start_date , name_com_ar , name_com_en,
question_en,answer_ar1,answer_ar2,answer_ar3
,answer_en1,answer_en2,answer_en3,right_answer;
*/
itemHolder.item_id.setText(currentItem.getPrize_id());
itemHolder.item_description.setText(currentItem.getName_com_ar());
itemHolder.start_date.setText(currentItem.getStart_date());
itemHolder.end_date.setText(currentItem.getEnd_date());
itemHolder.name_com_ar.setText(currentItem.getName_com_ar());
itemHolder.name_com_en.setText(currentItem.getName_com_en());
itemHolder.answer_en1.setText(currentItem.getAnswer_en1());
itemHolder.answer_en2.setText(currentItem.getAnswer_en2());
itemHolder.answer_en3.setText(currentItem.getAnswer_en3());
itemHolder.answer_ar1.setText(currentItem.getAnswer_ar1());
itemHolder.answer_ar2.setText(currentItem.getAnswer_ar2());
itemHolder.answer_ar3.setText(currentItem.getAnswer_ar3());
itemHolder.right_answer.setText(currentItem.getRight_answer());
itemHolder.question_en.setText(currentItem.getQuestion_en());
itemHolder.question_ar.setText(currentItem.getQuestion_ar());
itemHolder.desc_ar.setText(currentItem.getPrize_desc_ar());
itemHolder.desc_en.setText(currentItem.getPrize_desc_en());
String urlLogo = currentItem.getPrize_pic1();
loadImages(urlLogo, itemHolder);
setDefferinceTimer(itemHolder , currentItem.getEnd_date());
if (updateFuture == null) {
final Handler mainHandler = new Handler(Looper.getMainLooper());
updateFuture = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
setDefferinceTimer(itemHolder , currentItem.getEnd_date());
notifyDataSetChanged();
mainHandler.post(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
}
}, 0, 1000, TimeUnit.MILLISECONDS);
}
/* new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
startCountDown(itemHolder, currentItem.getEnd_date() + " 00:00:00");
notifyDataSetChanged();
}
public void onFinish() {
//counterTextView.setText("done!");
}
}.start();
}
});
*/
}
public void setDefferinceTimer(final RecyclerView.ViewHolder holder , String itemEndDate){
final ItemHolder itemHolder = (ItemHolder) holder;
current_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
// reachableDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(itemEndDate);
try {
d1 = format.parse(current_date);
d2 = format.parse(itemEndDate+" 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
diff = d2.getTime() - d1.getTime();
diffSeconds = diff / 1000 % 60;
diffMinutes = diff / (60 * 1000) % 60;
diffHours = diff / (60 * 60 * 1000) % 24;
diffDays = diff / (24 * 60 * 60 * 1000);
itemHolder.days_tf.setText(""+diffDays);
itemHolder.hours_tf.setText(""+diffHours);
itemHolder.minutes_tf.setText(""+diffMinutes);
itemHolder.seconds_tf.setText(""+diffSeconds);
}
private void loadImages(String urlThumbnail, final RecyclerView.ViewHolder holder) {
final ItemHolder itemHolder = (ItemHolder) holder;
mImageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
itemHolder.item_image.setImageBitmap(response.getBitmap());
//holder.salon_gender.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
@Override
public int getItemCount() {
return mListItems.size();
}
private class ItemHolder extends RecyclerView.ViewHolder {
public TextView item_id, item_description, end_date,
start_date, name_com_ar, name_com_en, question_ar,
question_en, answer_ar1, answer_ar2, answer_ar3, answer_en1,
answer_en2, answer_en3, right_answer , desc_ar , desc_en;
public TextView days_tf, hours_tf, minutes_tf, seconds_tf;
public CircleImageView item_image;
public ItemHolder(View itemView) {
super(itemView);
start_date = (TextView) itemView.findViewById(R.id.start_date);
end_date = (TextView) itemView.findViewById(R.id.end_date);
name_com_ar = (TextView) itemView.findViewById(R.id.name_com_ar);
name_com_en = (TextView) itemView.findViewById(R.id.name_com_en);
question_en = (TextView) itemView.findViewById(R.id.question_en);
question_ar = (TextView) itemView.findViewById(R.id.question_ar);
desc_ar = (TextView) itemView.findViewById(R.id.desc_ar);
desc_en = (TextView) itemView.findViewById(R.id.desc_en);
answer_ar1 = (TextView) itemView.findViewById(R.id.answer_ar1);
answer_ar2 = (TextView) itemView.findViewById(R.id.answer_ar2);
answer_ar3 = (TextView) itemView.findViewById(R.id.answer_ar3);
answer_en1 = (TextView) itemView.findViewById(R.id.answer_en1);
answer_en2 = (TextView) itemView.findViewById(R.id.answer_en2);
answer_en3 = (TextView) itemView.findViewById(R.id.answer_en3);
right_answer = (TextView) itemView.findViewById(R.id.right_answer);
item_id = (TextView) itemView.findViewById(R.id.item_id);
item_description = (TextView) itemView.findViewById(R.id.item_description);
item_image = (CircleImageView) itemView.findViewById(R.id.item_image);
days_tf = (TextView) itemView.findViewById(R.id.days_tf);
hours_tf = (TextView) itemView.findViewById(R.id.hours_tf);
minutes_tf = (TextView) itemView.findViewById(R.id.minutes_tf);
seconds_tf = (TextView) itemView.findViewById(R.id.seconds_tf);
}
}
6条答案
按热度按时间h5qlskok1#
简单的解决方案是:
注:1.Holder是ViewHolder类的对象
2.创建类UpdateTimerThread实现Runnable
3.将日期和时间转换为Long并存储到lgetCreatedTime中
4.每隔1秒运行一次处理程序以计时
pvabu6sv2#
在这里您可以查看并下载Countdown timers in a RecyclerView的源代码
Activity_main.xml
Adapter_layout.xml
MainActivity.Java
自定义适配器:
smtd7mpg3#
在这里,我扩展了
AppCompatTextView
并在其中添加了CountDownTimer
。我正在处理onStop
和onStart
,所以CountDownTimer
不能在后台运行。然后将其用作项目视图XML中的普通视图,如下所示
然后在适配器内部,您将需要实现非常重要的方法
onViewRecycled
,该方法将被调用以从Timer对象中释放内存。然后在夹子里,你会这样做
然后在活动/片段中,您必须在onStop中将Receierview设置为空,并在onStart中再次将其重新附加,这样做将在您的所有视图上调用
onViewRecycled
,如果屏幕上看不到回收器,则计时器将停止。r1zk6ea14#
在
ViewHolder
中添加CountDownTimer
成员。在onBindViewHolder()
中设置并启动计数器,不要忘记取消ViewHolder
相同示例中的任何现有计数器。在onTick()
中,您需要更新显示屏上的值,而不是启动计数器。u91tlkcl5#
可以在ViewHolder类中创建CountDownTimer引用,并在Adapter的onBindViewHolder方法中创建其示例。
它工作得很完美。
考验我自己。
bbmckpt76#
在onbindvieWHOLDER:
仍然是滞后的