简体   繁体   English

如何在RecyclerView中使用CountDownTimer?

[英]How to use a CountDownTimer in a RecyclerView?

In my application i should use CountDownTimer into recyclerView and for this i write below codes! 在我的应用程序中,我应该使用CountDownTimer进入recyclerView ,为此,我编写以下代码!
But when scroll recyclerView items, duplicate items data. 但是当滚动recyclerView项目时, 重复项目数据。
First please see my logCat and image : 首先请查看我的logCatimage
LogCat : 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

My app image : 我的应用图片:

在此处输入图片说明

In my logCat just two items has time > 0 and it's ok, and other items has time < 0 . 在我的logCat只有两个项目的time > 0没关系,而其他项目的time < 0
But why when running application show timer in other items! 但是为什么在运行应用程序时在其他项目中显示计时器!
In logCat item 8 has E/timerLogList: -8746 but in image show 00:06:30 ! logCat项目8具有E / timerLogList:-8746,但在图像显示中00:06:30

Should show Finished label . 应显示完成标签

My Activity codes : 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 : 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;
        }
    }
}

How can i fix it? 我该如何解决? please help me, because i really need your helps. 请帮助我,因为我真的需要您的帮助。
Thanks 谢谢

Override getItemId method in your adapter to avoid duplication of items during scroll. 重写adapter getItemId方法,以避免滚动期间重复项。 Also you will have to set setHasStableIds to true. 另外,您还必须将setHasStableIds设置为true。

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

mAdapter.setHasStableIds(true)

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

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