Set counter inside RecyclerView

后端 未结 2 1741
北恋
北恋 2021-01-15 14:24

I have count down timer as image below :

now these timers will start count down in each item, i tried many times to change this from my Adapter

2条回答
  •  太阳男子
    2021-01-15 14:37

    Here you can find the source code of Countdown Timer in Recycler Listview here

    Adapter.java

    import android.os.CountDownTimer;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    public class Adapter extends RecyclerView.Adapter{
    
    private ArrayList al_data;
    
    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView tv_timer;
        CountDownTimer timer;
    
        public MyViewHolder (View view){
            super(view);
            tv_timer = (TextView)view.findViewById(R.id.tv_timer);
    
        }
    
    
    }
    
    public Adapter(ArrayList al_data) {
        this.al_data = al_data;
    }
    
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout,parent,false);
    
    
        return new MyViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
    
        holder.tv_timer.setText(al_data.get(position));
    
        if (holder.timer != null) {
            holder.timer.cancel();
        }
         long timer = Long.parseLong(al_data.get(position));
    
        timer = timer*1000;
    
        holder.timer = new CountDownTimer(timer, 1000) {
            public void onTick(long millisUntilFinished) {
              holder.tv_timer.setText("" + millisUntilFinished/1000 + " Sec");
            }
    
            public void onFinish() {
                holder.tv_timer.setText("00:00:00");
            }
        }.start();
    
    
    }
    
    @Override
    public int getItemCount() {
        return al_data.size();
    }
    
    
    
    }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题