[Need HELP] How to add Selection to RecyclerView with binding

I need your help.

I have a recycler view with data-binding and try to add MultiSelection to it but have no clue, how to do it.

With the Multiselection, I will let the user edit or delete the transaction from the Room database. If you need more code, I can add it.

This is my adapter

private val ITEM_VIEW_TYPE_HEADER = 0 private val ITEM_VIEW_TYPE_ITEM = 1 class TransactionListAdapter( private val onDeleteCallback: (TransactionDao.TransactionCat) -> Unit ) : ListAdapter<DataItem, RecyclerView.ViewHolder>(TransactionDiffCallback()) { private val adapterScope = CoroutineScope(Dispatchers.Default) override fun onCreateViewHolder( parent: ViewGroup, viewType: Int ): RecyclerView.ViewHolder { return when (viewType) { ITEM_VIEW_TYPE_HEADER -> TextViewHolder.from(parent) ITEM_VIEW_TYPE_ITEM -> ViewHolder.from(parent) else -> throw ClassCastException("Unknown viewType $viewType") } } fun addHeaderAndSubmitList(list: List<TransactionDao.TransactionCat>?) { val date = SimpleDateFormat("dd MMM yyyy - EEEE", Locale.getDefault()) adapterScope.launch { val items = when (list) { null -> listOf(DataItem.Header("Loading")) else -> { val groupedList = list.groupBy { date.format(it.transaction.date) } var myList = ArrayList<DataItem>() for (i in groupedList.keys) { myList.add(DataItem.Header(i)) for (v in groupedList.getValue(i)) { myList.add(DataItem.TransactionCatItem(v)) } } myList } } withContext(Dispatchers.Main) { submitList(items) } } } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { when (holder) { is ViewHolder -> { val item = getItem(position) as DataItem.TransactionCatItem holder.bind(item.transactionCat) holder.binding.cvItemMainLastExpenses.setBackgroundResource(R.drawable.cnb_color_round) holder.binding.tvItemMainLastExpensesTitle.setOnLongClickListener { onDeleteCallback(item.transactionCat) true } holder.binding.tvItemMainLastExpensesDate.setOnLongClickListener { onDeleteCallback(item.transactionCat) true } holder.binding.ivItemMainLastExpenseArrow.setOnLongClickListener { onDeleteCallback(item.transactionCat) true } holder.binding.tvItemMainLastExpensesAmount.setOnLongClickListener { onDeleteCallback(item.transactionCat) true } holder.binding.cvItemMainLastExpenses.setOnLongClickListener { onDeleteCallback(item.transactionCat) true } holder.binding.ivTransaction.setOnLongClickListener { onDeleteCallback(item.transactionCat) true } } is TextViewHolder -> { val headerItem = getItem(position) as DataItem.Header holder.bind(headerItem.headerTrans) } } } class TextViewHolder private constructor(val binding: ItemMainLastExpensesHeaderBinding) : RecyclerView.ViewHolder(binding.root){ fun bind(headerTrans: String){ binding.headerTrans = headerTrans binding.executePendingBindings() } companion object { fun from(parent: ViewGroup): TextViewHolder { val layoutInflater = LayoutInflater.from(parent.context) val binding = ItemMainLastExpensesHeaderBinding.inflate(layoutInflater, parent, false) return TextViewHolder(binding) } } } override fun getItemViewType(position: Int): Int { return when(getItem(position)){ is DataItem.Header -> ITEM_VIEW_TYPE_HEADER is DataItem.TransactionCatItem -> ITEM_VIEW_TYPE_ITEM } } class ViewHolder private constructor(val binding: ItemMainLastExpensesBinding) : RecyclerView.ViewHolder(binding.root) { fun bind(item: TransactionDao.TransactionCat) { binding.transactionCat = item binding.executePendingBindings() } companion object { fun from(parent: ViewGroup): ViewHolder { val layoutInflater = LayoutInflater.from(parent.context) val binding = ItemMainLastExpensesBinding.inflate(layoutInflater, parent, false) return ViewHolder(binding) } } } } class TransactionDiffCallback : DiffUtil.ItemCallback<DataItem>() { override fun areItemsTheSame(oldItem: DataItem, newItem: DataItem): Boolean { return oldItem.id == newItem.id } override fun areContentsTheSame(oldItem: DataItem, newItem: DataItem): Boolean { return oldItem == newItem } } sealed class DataItem { abstract val id: Long data class TransactionCatItem(val transactionCat: TransactionDao.TransactionCat): DataItem() { override val id = transactionCat.transaction.transactionId } data class Header(val headerTrans: String) : DataItem() { override val id = headerTrans.hashCode().toLong() } } 

Thanks for help

submitted by /u/ekaya999
[link] [comments]