Sadashiv
Sadashiv

Reputation: 851

change clicked item color in reccyclerview

I am using recyclerview for showing list. I want to change color of selected item in recyclerview. I am getting color on selection of item, but after scrolling multiple items getting same color in list. Please give any suggestion

Upvotes: 0

Views: 122

Answers (1)

Sadashiv
Sadashiv

Reputation: 851

I got solution

I made global variable to store position and handled click listener in ViewHolder. Onclick of item, I changed the global position value

textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { globalPosition=getAdapterPosition(); notifyDataSetChanged(); } });

then in onBindViewHolder

if(postion==globalPosition)
{
    //change color to red
    textview.setTextColor(Color.RED);
}
else
{
    //change color to white of remaining items in list
    textview.setTextColor(Color.WHITE);
}

Upvotes: 1

Related Questions