简体   繁体   English

如何在每个 recyclerview 项目之间添加延迟?

[英]How to add delay between each recyclerview item?

I want to set delay time between each of my recyclerview items.我想在我的每个 recyclerview 项目之间设置延迟时间。 I have a List of the specific time delay I want for each item.我有一个我想要的每个项目的特定时间延迟列表。 Now using the following code it works fine for fewer items but if there are 15-20 items then multiple items will start at the same time.现在使用以下代码,它适用于较少的项目,但如果有 15-20 个项目,则多个项目将同时启动。 I'm assuming it's because as other items are not displayed it considers them as the first item and starts the timer.我假设这是因为其他项目没有显示,它认为它们是第一个项目并启动计时器。

holder.timer.postDelayed(new Runnable() {
            @Override
            public void run() {
                new CountDownTimer((long) (time.get(holder.getAdapterPosition()) * 60000), 1000) {
                    public void onTick(long millisUntilFinished) {
//timer.setText("minutes remaining: " + millisUntilFinished);
//here you can have your logic to set text to edittext
                        holder.timer.setText("" + String.format(FORMAT,
                                TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
                                        TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
                                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
                    }
                    public void onFinish() {
                        holder.timer.setText("done!");
                    }
                }.start();
            }
        },  delay* 60000);

Now this works fine for 4-5 items but for 10-15 items when I scroll 2 or 3 item's timer start at the same time instead of waiting for the previous item to finish现在这适用于 4-5 个项目,但对于 10-15 个项目,当我滚动 2 或 3 个项目的计时器同时启动而不是等待上一个项目完成时

just initialize the recyclerview with only one item,只需用一项初始化recyclerview,

then count the time with System.currentTimeMillis();然后用System.currentTimeMillis(); , ,

after two second add an item in the recyclerView list and call notifyItemInserted() to notify the adapter that a new item is inserted.两秒钟后在 recyclerView 列表中添加一个项目并调用notifyItemInserted()以通知适配器插入了一个新项目。

something like this:像这样的东西:

long initialTime = System.currentTimeMillis();
long now = System.currentTimeMillis();
while(now - initialTime < 2000){
    now = System.currentTimeMillis();
}
//now code after two seconds..
mainList.add(item);
adapter.notifyItemInserted();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM