'How can I reset button drawable in RecyclerViewAdapter?
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)
}
Solution 1:[1]
holder.itemName.setOnClickListener {
if (position==itemCount){
// the last one
}
}
Solution 2:[2]
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)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LiangKe |
| Solution 2 | Cuong Truong Quoc |
