Home > Software design >  How can I reset button drawable in RecyclerViewAdapter?
How can I reset button drawable in RecyclerViewAdapter?

Time:03-23

I have a recyclerView List that's color is white. Just there is a textView that's name is itemName in card design. When I click on an item, I want the color of that item to be orange. When I click on another item, I want the previous item to return to its main color, the color of the last item I clicked on to be orange. I think onBindViewHolder function is enough for this. How can I do this ?

 holder.itemName.setOnClickListener {
       holder.itemName.setBackgroundResource(R.drawable.custom_button)
 }

CodePudding user response:

    holder.itemName.setOnClickListener {
        if (position==itemCount){
           // the last one
        }
    }

CodePudding user response:

You must call notifyDataSetChanged in onItemClickListener

Ex:

holder.itemName.setOnClickListener {
       holder.itemName.setBackgroundResource(R.drawable.custom_button)
       notifyDataSetChanged()
 }

in onBindViewHolder:

if (position == positionSelected) {
 holder.itemName.setBackgroundResource(R.drawable.orange)
} else {
 holder.itemName.setBackgroundResource(R.drawable.white)
}
  • Related