diff --git a/app/src/main/kotlin/jackall/moncalc/adapter/HistoryRecyclerAdapter.kt b/app/src/main/kotlin/jackall/moncalc/adapter/HistoryRecyclerAdapter.kt new file mode 100644 index 0000000..c92c058 --- /dev/null +++ b/app/src/main/kotlin/jackall/moncalc/adapter/HistoryRecyclerAdapter.kt @@ -0,0 +1,56 @@ +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.View +import android.view.ViewGroup +import jackall.moncalc.R +import jackall.moncalc.databinding.HistoryListItemBinding +import jackall.moncalc.viewmodel.HistoryItemViewModel +import jackall.moncalc.vo.HistoryItem + +/** + * Created by matsumoto_k on 2017/11/03. + */ +class HistoryRecyclerAdapter(var context: Context) : RecyclerView.Adapter() { + + var items: ArrayList = ArrayList() + + inner class ViewHolder(itemView: View, viewModel: HistoryItemViewModel) : RecyclerView.ViewHolder(itemView) { + var viewModel: HistoryItemViewModel? = null + + init { + this.viewModel = viewModel + } + + fun loadItem(item: HistoryItem) { + viewModel?.loadItem(item) + } + } + + fun setItemAndRefresh(items: ArrayList) { + this.items = items + notifyDataSetChanged() + } + + fun getItemAt(position: Int): HistoryItem { + return items.get(position) + } + + override fun onBindViewHolder(holder: ViewHolder?, position: Int) { + val item = getItemAt(position) + holder?.loadItem(item) + } + + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { + val binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.history_list_item, parent, false) + binding.historyItemViewModel = HistoryItemViewModel() + return ViewHolder(binding.root, binding.historyItemViewModel!!) + } + + override fun getItemCount(): Int { + return items.size + } +} \ No newline at end of file