Newer
Older
OverlayMultiCounter / app / src / main / kotlin / jackall / overlaymulticounter / CounterAdapter.kt
MatsumotoKeiju on 9 Nov 2017 1 KB [add] Counter adapter
package jackall.overlaymulticounter

import android.content.Context
import android.databinding.DataBindingUtil
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import jackall.overlaymulticounter.databinding.ListCounterItemBinding
import jackall.overlaymulticounter.viewmodel.CounterItemViewModel

/**
 * Created by matsumoto_k on 2017/11/08.
 */
class CounterAdapter(val context: Context, val items: ArrayList<CounterItemViewModel>) : RecyclerView.Adapter<CounterAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val binding = DataBindingUtil.inflate<ListCounterItemBinding>(LayoutInflater.from(context), R.layout.list_counter_item, parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        holder?.binding?.counterItemViewModel = items.get(position)
    }

    override fun getItemCount(): Int {
        return items.count()
    }

    fun insert(item: CounterItemViewModel) {
        items.add(item)
        notifyItemInserted(itemCount)
    }

    fun remove(position: Int) {
        items.removeAt(position)
        notifyItemRemoved(position)
    }

    inner class ViewHolder(val binding: ListCounterItemBinding) : RecyclerView.ViewHolder(binding.root)
}