-1

I'm stuck on a little problem.

When I click on a row in my RecyclerView, I'd like to change its color to some other color (i.e.: red).
Then, if I click this row again, I would like to set its color back to the normal one (i.e.: white).
Then, if I click on a second row or n-th row, I would like to do the same.

I've tried to work on the RecyclerView Adapter class, in my ViewHolder I tried some ideas counting user clicks, setting a boolean to check the colored rows, and getAdapterPosition to know what is the row's position... but actually all my tries failed!

Would you please help me with that problem?

It seems that I'm close to the solution, but need a little help

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

You're going to need to keep track of which items were selected on the list (otherwise any changes to the view will simply be recycled).

Keep a list of selected items within your adapter. In the adapter's onBindViewHolder you can check against the list and if the list contains the view/item, you can color the view accordingly.

Edit: Refer here for a working example How to properly highlight selected item on RecyclerView?

Community
  • 1
  • 1
jak10h
  • 519
  • 4
  • 11
  • `holder.itemsParent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mylist.add(holder.serviceNameAdded.getText().toString()); } });` if you mean like this , I think it will add every click on the row to the list So , How I can handle user's first click and second click ?! – Karim Abd Elhameed Mohamed Oct 07 '16 at 17:22
  • You'll need to pass your onClickListener into the adapter for the adapter to set on each view – jak10h Oct 07 '16 at 17:31
  • [ https://ideone.com/FLsH0F ] Would u please , check this ? I think that is my old solution – Karim Abd Elhameed Mohamed Oct 07 '16 at 17:44
  • broken link (404) – jak10h Oct 07 '16 at 17:45
  • That's the viewholder, which the listener does need to go to so that's fine. But you need to add a list and the logic into the adapter itself that uses those viewholders. – jak10h Oct 07 '16 at 17:51
  • Ummmmm I feel dizzy , It seems that I've misunderstanding would you please provide me with such an example ? Here is my whole Adapter Class : [ https://ideone.com/RuxAIy ] – Karim Abd Elhameed Mohamed Oct 07 '16 at 17:59