Selection zur RecyclerView mit Binding hinzufügen

E

ekaya999

Neues Mitglied
0
Hallo,

ich möchte MultiSelection zu meinem RecyclerView hinzufügen. Es gibt zwar viele Tutorials im Internet, sie passen aber einfach nicht zu meinem RecyclerView.
Wäre für jede Hilfe sehr dankbar.

Hier ist mein Adapter:

Code:
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()

    }

}
 
Hallo @ekaya999

ich persönlich führe die Selection in meinem Ursprungs-Array mit (Flag) und visualisiere die Selection nach Status.
Das hat den Vorteil bei der späteren Verarbeitung und Sortierung der Liste.
 
Hallo @swa00 ,

danke für den Hinweis aber kannst du mir es rin bisschen detaillierter erklären.. Bin ziemlich neu beim Programmieren..
Danke!
 
Zuletzt bearbeitet:

Ähnliche Themen

J
Antworten
5
Aufrufe
929
swa00
swa00
B
Antworten
4
Aufrufe
487
bb321
B
FabianDev
Antworten
5
Aufrufe
555
swa00
swa00
Zurück
Oben Unten