0

私は数日間問題を扱っています。私はrecyclerView(ストップウォッチを持つユーザーのリスト)にストップウォッチのようなものがあります。問題は、私はそれをBindViewHolderでどのように同期させるのか、毎秒1秒を時間に追加する方法を知らないことです。 編集:ストップウォッチクラスを追加して実装しました。それはうまくいっていますが、私がスクロールするとすべてが乱雑です。任意のアイデアをどのように改善するのですか?リサイクラーのストップウォッチ

マイアダプタ

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 
    } 
} 

ストップウォッチ:

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() { 
     ... 
    } 
} 

答えて

-1

あなたはすべて、実行時のコールインクリメントストップウォッチ/ S + Notifydatasetchanged()を呼び出して1000ミリ秒のPostdelay、とのRunnableを使用することができます。

+0

私はちょうどcoutdownをテストしましたが、残念ながらそれは反対のアプローチです:Dあなたはいくつかのスニペットを作れますか? – Stepan

関連する問題