0

Je fais face à un problème pendant plusieurs jours. J'ai quelque chose comme chronomètre dans recyclerView (une liste d'utilisateurs qui ont chronomètre). Le problème est que je ne sais pas comment le synchroniser sur BindViewHolder et comment ajouter chaque seconde 1 seconde à leur temps. Édité: J'ai ajouté la classe chronomètre et l'ai implémentée. Cela fonctionne bien, mais quand je défile, tout est salissant. Des idées pour l'améliorer?Chronomètre dans recycleurVoir

Mon adaptateur

class DashboardAdapter(var context: Context?) : RecyclerView.Adapter<DashboardAdapter.MyViewHolder>() { 

    lateinit var dataSource: List<User> 

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { 

     var stopwatch: Stopwatch? = null 

     val name = view.findViewById(R.id.name) as TextView 
     val avatar = view.findViewById(R.id.profile_image) as ImageView 
     val project = view.findViewById(R.id.project) as TextView 
     val playImage = view.findViewById(R.id.play) as ImageView 
     val time = view.findViewById(R.id.time) as TextView 
     val placement = view.findViewById(R.id.placement) as TextView 
    } 

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) { 
      holder.stopwatch = Stopwatch(object : Stopwatch.StopWatchListener { 
      override fun onTick(time: String) { 
       holder.time.text = time 
     } 
    }) 
      holder.stopwatch?.start(time.toLong()) 

    } 

    override fun getItemCount(): Int = dataSource.size 

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder { 
     val itemView = LayoutInflater.from(parent?.context).inflate(R.layout.item_dashboard_workers, parent, false) 
     return MyViewHolder(itemView) 
    } 

    private fun getRealHolderPosition(viewHolder: MyViewHolder) : Int = viewHolder.adapterPosition + 1 

    fun setUsers(dataSource: List<User>) { 
     this.dataSource = dataSource 
    } 
} 

Chronomètre:

class Stopwatch(listener: StopWatchListener) { 

    interface StopWatchListener { 
     fun onTick(time: String) 
    } 

    private val DELAY : Long = 1000 

    private var startTime: Long = 0 

    var isRunning = false 
     private set 

    private var currentTime: Long = 0 

    ... 

    /** 
    * Starts the stopwatch from an offset 
    * @param offset The offset in milli seconds 
    */ 
    fun start(offset: Long) { 
     stop() 
     this.startTime = System.currentTimeMillis() - offset 
     this.isRunning = true 
     val handler = Handler() 
     handler.post(object : Runnable { 
      override fun run() { 
       //lock to access running 
       synchronized([email protected]) { 
        if (isRunning) { 
         listener?.onTick([email protected]()) 
         handler.postDelayed(this, DELAY) 
        } 
       } 
      } 
     }) 
    } 

    @Synchronized 
    private fun stop() { 
     this.isRunning = false 
    } 

    private fun restart() { 
     this.stop() 
     this.start() 
    } 

    @Synchronized 
    private fun pause() { 
     ... 
    } 

    private fun resume() { 
     ... 
    } 
} 

Répondre

-1

Vous pouvez utiliser un Runnable avec Postdelay de 1000ms, incrément d'appel chronomètre/s à chaque course + appel Notifydatasetchanged();

+0

Je viens de tester la méthode de décompte et ça fonctionne très bien, malheureusement c'est l'approche inverse: D Pourriez-vous faire un extrait? – Stepan