java - 如何在 RecyclerView 中使用 CountDownTimer?

标签 java android android-recyclerview

在我的应用程序中,我应该使用 CountDownTimer 进入 recyclerView 为此我编写了以下代码!
但是当滚动 recyclerView 项,重复 项数据。
首先请看我的logCat image :
LogCat:

05-23 12:49:50.827 8191-8191/com.example.mac8.testapps E/timerLogList: 311
05-23 12:49:50.830 8191-8191/com.example.mac8.testapps E/timerLogList: 611
05-23 12:49:50.833 8191-8191/com.example.mac8.testapps E/timerLogList: -8746
05-23 12:49:50.836 8191-8191/com.example.mac8.testapps E/timerLogList: -8746
05-23 12:49:50.839 8191-8191/com.example.mac8.testapps E/timerLogList: -8746
05-23 12:49:50.842 8191-8191/com.example.mac8.testapps E/timerLogList: -8746
05-23 12:49:50.845 8191-8191/com.example.mac8.testapps E/timerLogList: -8746
05-23 12:49:50.848 8191-8191/com.example.mac8.testapps E/timerLogList: -8746
05-23 12:49:50.851 8191-8191/com.example.mac8.testapps E/timerLogList: -8746
05-23 12:49:50.854 8191-8191/com.example.mac8.testapps E/timerLogList: -8745
05-23 12:50:02.075 8191-8191/com.example.mac8.testapps E/timerLogList: -8756
05-23 12:50:02.110 8191-8191/com.example.mac8.testapps E/timerLogList: -8756
05-23 12:50:02.143 8191-8191/com.example.mac8.testapps E/timerLogList: -8756
05-23 12:50:02.160 8191-8191/com.example.mac8.testapps E/timerLogList: -8756
05-23 12:50:02.173 8191-8191/com.example.mac8.testapps E/timerLogList: -8756
05-23 12:50:02.190 8191-8191/com.example.mac8.testapps E/timerLogList: -8756
05-23 12:50:02.240 8191-8191/com.example.mac8.testapps E/timerLogList: -8756

我的应用程序图像:

enter image description here

在我的 logCat 只有两项有 time > 0 还可以,其他项目有 time < 0
但是为什么运行应用程序时在其他项目中显示计时器!
logCat第 8 项具有 E/timerLogList: -8746,但在图像中显示 00:06:30!

应显示成品标签

<强> My Activity codes :

public class TimerRecyclerActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ProgressBar timerProgressBar;
    private List<TodayGmodel> model = new ArrayList<>();
    private Adapter adapter;
    private ApiInterface api;
    private LinearLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer_recycler);

        api = ApiClient.getClient().create(ApiInterface.class);
        adapter = new Adapter(getApplicationContext(), model);
        recyclerView = findViewById(R.id.timerRecyclerView);
        timerProgressBar = findViewById(R.id.timerProgressBar);
        layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        Call<MainResponse> call = api.getMainAuctions("");
        call.enqueue(new Callback<MainResponse>() {
            @Override
            public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
                if (response.isSuccessful()) {
                    if (response.body().getRes() != null) {
                        if (response.body().getRes().getToday().size() > 0) {
                            timerProgressBar.setVisibility(View.GONE);
                            model.clear();
                            model.addAll(response.body().getRes().getToday());
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<MainResponse> call, Throwable t) {

            }
        });
    }
}

<强> Adapter codes :

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

    private List<TodayGmodel> model;
    private Context context;

    public Adapter(Context context, List<TodayGmodel> model) {
        this.model = model;
        this.context = context;
    }

    @Override
    public Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.timer_adapter_layout, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final Adapter.ViewHolder viewHolder, int position) {
        TodayGmodel todayGmodel = model.get(position);
        viewHolder.refreshTime(todayGmodel.getCalculateEnd());

        Log.e("timerLogList", "" + model.get(position).getCalculateEnd());
    }

    @Override
    public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        int pos = holder.getAdapterPosition();
        TodayGmodel todayGmodel = model.get(pos);

        holder.refreshTime(todayGmodel.getCalculateEnd());
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
        if (holder.getDownTimer() != null)
            holder.getDownTimer().cancel();
    }

    @Override
    public int getItemCount() {
        return model.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_timer;
        private CountDownTimer downTimer;
        private long remainTime = 0;

        public ViewHolder(View view) {
            super(view);
            tv_timer = view.findViewById(R.id.tv_timer);
        }

        public void refreshTime(long timer) {
            if (timer > 0) {
                downTimer = new CountDownTimer(timer * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        remainTime = millisUntilFinished / 1000;
                        int seconds = (int) (millisUntilFinished / 1000);
                        int hours = seconds / (60 * 60);
                        int tempMint = (seconds - (hours * 60 * 60));
                        int minutes = tempMint / 60;
                        seconds = tempMint - (minutes * 60);
                        tv_timer.setText(String.format("%02d", hours)
                                + ":" + String.format("%02d", minutes)
                                + ":" + String.format("%02d", seconds));
                        if (remainTime > 50) {
                            tv_timer.setBackgroundColor(ContextCompat.getColor(context, R.color.color1));
                        } else if (remainTime < 50 && remainTime > 20){
                            tv_timer.setBackgroundColor(ContextCompat.getColor(context, R.color.color2));
                        } else {
                            tv_timer.setBackgroundColor(ContextCompat.getColor(context, R.color.color4));
                        }
                    }

                    @Override
                    public void onFinish() {

                    }
                }.start();
            } else {
                tv_timer.setText("Finish");
                if (downTimer != null)
                    downTimer.cancel();
            }
        }

        public CountDownTimer getDownTimer() {
            return downTimer;
        }
    }
}

我该如何修复它?请帮助我,因为我真的需要你的帮助。
谢谢

最佳答案

重写适配器中的getItemId方法以避免滚动期间出现重复的项目。此外,您还必须将 setHasStableIds 设置为 true。

@Override
public long getItemId(int position) {
    return position;
}

mAdapter.setHasStableIds(true)

关于java - 如何在 RecyclerView 中使用 CountDownTimer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50484833/

相关文章:

java - 使用任何开源代码或工具将大型机二进制文件转换为 ASCII

android - 您可以在 Android 的自定义 View 中使用 SensorEventListener 吗?

java - 如何在 recyclerview 项目中获取 TextView 的值?

java - 有没有办法在不重新绑定(bind)的情况下更新 RecyclerView 中的某些项目?

java - Semaphore 可以安全地使用双重检查锁定习惯用法吗?

java - 如何从 ifs 文本文件中提取 2D double 组 (Java)

android - 获取选定复选框android的文本?

android - 我应该如何为 RecyclerView 实现排序?

java - JSON 键的多个值

android - 使用 Pipe (Aerogear) 调用 api 时获取状态代码 404 和服务连接错误