Home > database >  Change Background color of recyclerview items while selecting and deselecting
Change Background color of recyclerview items while selecting and deselecting

Time:10-06

I am able to change the background color but my main issue is that it is changing the Background color only for alternate Recyclerview items(Not for each and every item.)

eg:(changing color of 1st,3rd & 5th item but not for 2nd and 4th )

Below is my adapter class code snippet:

class NewListAdapter(
    var activity: NewFragment,
    var itemListner: AllNewListener
) : ListAdapter<AllContactsSaveList, NewListAdapter.OutagesViewHolder>(DiffCallback())
{

    var lastText: String? = ""
    var isSelected: Boolean? = null

    lateinit var layoutInflater: LayoutInflater

    class DiffCallback : DiffUtil.ItemCallback<AllContactsSaveList>() {
        override fun areItemsTheSame(oldItem: AllContactsSaveList, newItem: AllContactsSaveList) =
            oldItem.flagsort == newItem.flagsort

        override fun areContentsTheSame(
            oldItem: AllContactsSaveList,
            newItem: AllContactsSaveList
        ) =
            oldItem == newItem
    }

    interface AllChatsNewListener {
        fun onNewAllChatsClick(
            name: String?,
            id: String?,
            position: Int
        )
    }


    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): OutagesViewHolder {
        return OutagesViewHolder(
            SelectListItemsBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    inner class OutagesViewHolder(viewBinding: SelectListItemsBinding) :
        RecyclerView.ViewHolder(viewBinding.root) {
        val sender_msg: TextView = itemView.sender_msggfr
        val checkgreenicon: ShapeableImageView = itemView.checkgreenicon
    }


    override fun onBindViewHolder(holder: OutagesViewHolder, position: Int) {
        holder.apply {
            val o: AllContactsSaveList = getItem(position)

        
            itemView.setOnClickListener {
                if(isSelected == true){
                    isSelected = false
                    itemView.setBackgroundColor(Color.parseColor("#00000000"))
                    itemView.checkgreenicon.visibility = View.GONE
                }
                else{
                    isSelected = true
                    itemView.setBackgroundColor(Color.parseColor("#2E707070"))
                    itemView.checkgreenicon.visibility = View.VISIBLE
                    onlinecolor.visibility = View.GONE
                }
                notifyItemChanged(position)
                itemListner.onNewAllChatsClick(
                    o.name,
                    Position
                )
            }
        }
    }

What Changes should i make or edit such that i am able to change the background color of each item instead of alternate items.

CodePudding user response:

isSelected should be a data of AllContactsSaveList object, and when you create object, set default isSeclected = false. Then in your OnBindViewHolder, change it like this:

            itemView.setOnClickListener {
                o.isSelected = !o.isSelected
                if(o.isSelected == true){
                    itemView.setBackgroundColor(Color.parseColor("#00000000"))
                    itemView.checkgreenicon.visibility = View.GONE
                }
                else{
                    itemView.setBackgroundColor(Color.parseColor("#2E707070"))
                    itemView.checkgreenicon.visibility = View.VISIBLE
                    onlinecolor.visibility = View.GONE
                }
                notifyItemChanged(position)
                itemListner.onNewAllChatsClick(
                    o.name,
                    Position
                )
            }
  • Related