diff --git a/app/src/main/kotlin/jackall/moncalc/adapter/RegistAdapter.kt b/app/src/main/kotlin/jackall/moncalc/adapter/RegistAdapter.kt new file mode 100644 index 0000000..139ff75 --- /dev/null +++ b/app/src/main/kotlin/jackall/moncalc/adapter/RegistAdapter.kt @@ -0,0 +1,42 @@ +package jackall.moncalc.adapter + +import android.content.Context +import android.databinding.DataBindingUtil +import android.support.v7.widget.RecyclerView +import android.view.LayoutInflater +import android.view.ViewGroup +import jackall.moncalc.R +import jackall.moncalc.databinding.RegistItemBinding +import jackall.moncalc.viewmodel.RegistItemViewModel + +/** + * Created by matsumoto_k on 2017/11/04. + */ +class RegistAdapter(val context: Context, val items: ArrayList) : RecyclerView.Adapter() { + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { + val binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.regist_item, parent, false) + return ViewHolder(binding) + } + + override fun onBindViewHolder(holder: ViewHolder?, position: Int) { + holder?.binding?.viewModel = items.get(position) + } + + override fun getItemCount(): Int { + return items.count() + } + + fun add(item: RegistItemViewModel) { + items.add(item) + notifyItemInserted(items.size - 1) + } + + fun remove() { + if (itemCount > 1) { + items.removeAt(itemCount - 1) + notifyItemRemoved(itemCount) + } + } + + inner class ViewHolder(val binding: RegistItemBinding) : RecyclerView.ViewHolder(binding.root) +} \ No newline at end of file