I created a Kotlin mobile app. In my app i use the RecyclerView
with adapter.
I want to changing the background color of item clicked in my RecyclerView
, and when i click on another item, the color will be change and the color of the first item clicked return to the default color.
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val itemCategory: ProductCategoryData = categories[position]
holder.categoryId.text = itemCategory.id.toString()
println(holder.categoryId.text)
println(itemCategory.name?.get("En").toString())
holder.categoryName.text = itemCategory.name?.get("En").toString()
............
holder.itemView.setOnClickListener {
rowindex = position
mListener?.onItemClick(holder.itemView, categories[position])
}
if (rowindex == position) {
holder.itemView.setBackgroundColor(Color.parseColor("#FED07A"))
} else {
holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"))
}
}
What should i change in my code to make it functional