title: Change background color of selected item in recyclerview
body: <p>I have a recycler view with linearlayout and textviews inside cardview. I would like to implement onclick method for my item where in I would like to change the background color of the linear layout and invert the textcolor of the textviews. I have got it working but the main issue I am facing is that if I select 1st item then 1st as well as 7th items color is changed. </p>
  
  <p>Trying to get this to work since two days. Any help would be appreciated.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Models.ViewModels;
  using Android.App;
  using Android.Content;
  using Android.Graphics;
  using Android.OS;
  using Android.Runtime;
  using Android.Support.V7.Widget;
  using Android.Views;
  using Android.Widget;
  
  namespace A.Droid.Adapters
  {
      public class DeliveryAdapter : RecyclerView.Adapter//, View.IOnClickListener
      {
          List&lt;RequestViewModel&gt; list;
          public Context v;
          public event EventHandler&lt;int&gt; phoneClick;
  
          List&lt;RequestViewModel&gt; selectedList = new List&lt;RequestViewModel&gt;();
  
          public DeliveryAdapter(List&lt;RequestViewModel&gt; records, Context v1)
          {
              list = records;
              v = v1;
          }
          public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
          {
              // Inflate the CardView for the photo:
              View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.activity_cardview_slinkyRecordList, parent, false);
  
              DeliveryListViewHolder vh = new DeliveryListViewHolder(itemView, OnPhoneClick);
              return vh;
          }
  
          // Fill in the contents of the photo card (invoked by the layout manager):
          public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
          {
              DeliveryListViewHolder viewHolder = holder as DeliveryListViewHolder;
              viewHolder.PICNumber.Text = string.IsNullOrEmpty(list[position].PICNumber) ? "-" : list[position].PICNumber;
              viewHolder.PropertyName.Text = string.IsNullOrEmpty(list[position].PropertyName) ? "-" : list[position].PropertyName;
              viewHolder.ContactAddress.Text = string.IsNullOrEmpty(list[position].ContactAddress) ? "-" : list[position].ContactAddress;
              viewHolder.ContactPerson.Text = string.IsNullOrEmpty(list[position].ContactPerson) ? "-" : list[position].ContactPerson;
              viewHolder.ContactNumber.Text = string.IsNullOrEmpty(list[position].ContactNumber) ? "-" : list[position].ContactNumber;
  
              viewHolder.NumberOfAliveSpecies.Text = list[position].NumberOfAliveStock + " Alive " + list[position].NameOfSpecies;
              viewHolder.NumberOfDeadSpecies.Text = list[position].NumberOfDeadStock + " Dead " + list[position].NameOfSpecies;
  
              viewHolder.DistanceOfTransporterToPIC.Text = list[position].DistanceOfTransporterFromPIC.ToString() + " KM"; //DeliveryList[position].DistanceOfTransporterFromPIC.ToString();
                                                                                                                           //viewHolder.MainLinearLayout.SetOnClickListener((new OnClickListener(viewHolder.MainLinearLayout,position));                                                                         // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
                                                                                                                           //viewHolder.MainLinearLayout.SetOnClickListener(this);                                                                        // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
  
              viewHolder.cardView.Click += delegate (object sender, EventArgs e)
              {
                  if (selectedList.All(i =&gt; i.slinkyStockRequestId != list[position].slinkyStockRequestId))
                  {
                      selectedList.Add(list[position]);
  
                      viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.Color.white));
  
                      viewHolder.PICNumber.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                      viewHolder.PropertyName.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                      viewHolder.ContactAddress.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                      viewHolder.ContactPerson.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                      viewHolder.ContactNumber.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
  
                      viewHolder.NumberOfAliveSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                      viewHolder.NumberOfDeadSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
  
                      viewHolder.DistanceOfTransporterToPIC.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
                      viewHolder.mapIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);
                      viewHolder.contactIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);
                      viewHolder.phoneIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);
  
                  }
                  else
                  {
                      selectedList.Remove(list[position]);
  
                      viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.Color.dark_blue));
  
                      viewHolder.PICNumber.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                      viewHolder.PropertyName.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                      viewHolder.ContactAddress.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                      viewHolder.ContactPerson.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                      viewHolder.ContactNumber.SetTextColor(v.Resources.GetColor(Resource.Color.white));
  
                      viewHolder.NumberOfAliveSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                      viewHolder.NumberOfDeadSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.white));
  
                      viewHolder.DistanceOfTransporterToPIC.SetTextColor(v.Resources.GetColor(Resource.Color.white));
                      viewHolder.mapIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);
                      viewHolder.contactIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);
                      viewHolder.phoneIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);
  
                  }
              };
  
  
  
              animate(holder);
          }
  
  
          public List&lt;RequestViewModel&gt; GetSelectedItems()
          {
              return selectedList;
          }
  
  
          // Return the number of photos available in the photo album:
          public override int ItemCount
          {
              get { return list.Count; }
          }
  
  
          // Raise an event when the phone-click takes place:
          void OnPhoneClick(int position)
          {
              if (phoneClick != null)
              {
                  phoneClick(this, position);
              }
          }
  
  
          public class DeliveryListViewHolder : RecyclerView.ViewHolder
          {
              public ImageView MapTag { get; private set; }
              public TextView PICNumber { get; private set; }
              public TextView PropertyName { get; private set; }
              public TextView ContactAddress { get; private set; }
              public TextView ContactPerson { get; private set; }
              public TextView ContactNumber { get; private set; }
              public TextView NameOfSpecies { get; private set; }
              public TextView NumberOfAliveSpecies { get; private set; }
              public TextView NumberOfDeadSpecies { get; private set; }
              public TextView DistanceOfTransporterToPIC { get; private set; }
              public CardView cardView { get; private set; }
              public LinearLayout MainLinearLayout { get; private set; }
              public ImageView mapIcon { get; private set; }
              public ImageView contactIcon { get; private set; }
              public ImageView phoneIcon { get; private set; }
              // Get references to the views defined in the CardView layout.
              public DeliveryListViewHolder(View itemView, Action&lt;int&gt; phoneClickListener) : base(itemView)
              {
                  MapTag = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.mapIcon);
                  PICNumber = itemView.FindViewById&lt;TextView&gt;(Resource.Id.PICNumber);
                  PropertyName = itemView.FindViewById&lt;TextView&gt;(Resource.Id.nameOfProperty);
                  ContactPerson = itemView.FindViewById&lt;TextView&gt;(Resource.Id.contactPerson);
                  NumberOfAliveSpecies = itemView.FindViewById&lt;TextView&gt;(Resource.Id.noOfAliveSpecies);
                  NumberOfDeadSpecies = itemView.FindViewById&lt;TextView&gt;(Resource.Id.noOfDeadSpecies);
                  DistanceOfTransporterToPIC = itemView.FindViewById&lt;TextView&gt;(Resource.Id.areaInKM);
                  ContactAddress = itemView.FindViewById&lt;TextView&gt;(Resource.Id.address);
                  ContactNumber = itemView.FindViewById&lt;TextView&gt;(Resource.Id.mobileNo);
                  mapIcon = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.mapIcon);
                  contactIcon = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.contactIcon);
                  phoneIcon = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.phoneIcon);
  
                  cardView = itemView.FindViewById&lt;CardView&gt;(Resource.Id.mainCardviewLayout);
                  MainLinearLayout = itemView.FindViewById&lt;LinearLayout&gt;(Resource.Id.MainLinearLayout);
                  ContactNumber.Click += (sender, e) =&gt; phoneClickListener(base.Position);
              }
          }
      }
  
  
  }
  </code></pre>
  
  <p><strong>Edit</strong>
  From <code>Deep Patel</code> input, here is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Models.ViewModels;
  using Android.App;
  using Android.Content;
  using Android.Graphics;
  using Android.OS;
  using Android.Runtime;
  using Android.Support.V7.Widget;
  using Android.Views;
  using Android.Widget;
  
  namespace A.Droid.Adapters
  {
      public class DeliveryAdapter : RecyclerView.Adapter//, View.IOnClickListener
      {
          List&lt;RequestViewModel&gt; list;
          public Context v;
          public event EventHandler&lt;int&gt; phoneClick;
  
          List&lt;RequestViewModel&gt; selectedList = new List&lt;RequestViewModel&gt;();
  
          public DeliveryAdapter(List&lt;RequestViewModel&gt; records, Context v1)
          {
              list = records;
              v = v1;
          }
          public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
          {
              // Inflate the CardView for the photo:
              View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.activity_cardview_slinkyRecordList, parent, false);
  
              DeliveryListViewHolder vh = new DeliveryListViewHolder(itemView, OnPhoneClick);
              return vh;
          }
  
          public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
      {
          DeliveryListViewHolder viewHolder = holder as DeliveryListViewHolder;
          viewHolder.PICNumber.Text = string.IsNullOrEmpty(list[position].PICNumber) ? "-" : list[position].PICNumber;
          viewHolder.PropertyName.Text = string.IsNullOrEmpty(list[position].PropertyName) ? "-" : list[position].PropertyName;
          viewHolder.ContactAddress.Text = string.IsNullOrEmpty(list[position].ContactAddress) ? "-" : list[position].ContactAddress;
          viewHolder.ContactPerson.Text = string.IsNullOrEmpty(list[position].ContactPerson) ? "-" : list[position].ContactPerson;
          viewHolder.ContactNumber.Text = string.IsNullOrEmpty(list[position].ContactNumber) ? "-" : list[position].ContactNumber;
  
          viewHolder.NumberOfAliveSpecies.Text = list[position].NumberOfAliveStock + " Alive " + list[position].NameOfSpecies;
          viewHolder.NumberOfDeadSpecies.Text = list[position].NumberOfDeadStock + " Dead " + list[position].NameOfSpecies;
  
          viewHolder.DistanceOfTransporterToPIC.Text = list[position].DistanceOfTransporterFromPIC.ToString() + " KM"; //DeliveryList[position].DistanceOfTransporterFromPIC.ToString();
                                                                                                                       //viewHolder.MainLinearLayout.SetOnClickListener((new OnClickListener(viewHolder.MainLinearLayout,position));                                                                         // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
                                                                                                                       //viewHolder.MainLinearLayout.SetOnClickListener(this);                                                                        // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
  
          viewHolder.cardView.Click += delegate (object sender, EventArgs e)
          {
  
              setSelectedPosition(position);
          };
  
          viewHolder.MainLinearLayout.SetBackgroundColor(list[position].isSelected() ? v.Resources.GetColor(Resource.Color.white) : v.Resources.GetColor(Resource.Color.dark_blue));
  
  
          animate(holder);
      }
  
      private void setSelectedPosition(int position)
      {
          for (int i = 0; i &lt; list.Count(); i++)
          {
              list[position].setSelected(i == position);
          }
          NotifyDataSetChanged();
      }
  
          public List&lt;RequestViewModel&gt; GetSelectedItems()
          {
              return selectedList;
          }
  
  
          // Return the number of photos available in the photo album:
          public override int ItemCount
          {
              get { return list.Count; }
          }
  
  
          // Raise an event when the phone-click takes place:
          void OnPhoneClick(int position)
          {
              if (phoneClick != null)
              {
                  phoneClick(this, position);
              }
          }
  
  
          public class DeliveryListViewHolder : RecyclerView.ViewHolder
          {
              public ImageView MapTag { get; private set; }
              public TextView PICNumber { get; private set; }
              public TextView PropertyName { get; private set; }
              public TextView ContactAddress { get; private set; }
              public TextView ContactPerson { get; private set; }
              public TextView ContactNumber { get; private set; }
              public TextView NameOfSpecies { get; private set; }
              public TextView NumberOfAliveSpecies { get; private set; }
              public TextView NumberOfDeadSpecies { get; private set; }
              public TextView DistanceOfTransporterToPIC { get; private set; }
              public CardView cardView { get; private set; }
              public LinearLayout MainLinearLayout { get; private set; }
              public ImageView mapIcon { get; private set; }
              public ImageView contactIcon { get; private set; }
              public ImageView phoneIcon { get; private set; }
              // Get references to the views defined in the CardView layout.
              public DeliveryListViewHolder(View itemView, Action&lt;int&gt; phoneClickListener) : base(itemView)
              {
                  MapTag = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.mapIcon);
                  PICNumber = itemView.FindViewById&lt;TextView&gt;(Resource.Id.PICNumber);
                  PropertyName = itemView.FindViewById&lt;TextView&gt;(Resource.Id.nameOfProperty);
                  ContactPerson = itemView.FindViewById&lt;TextView&gt;(Resource.Id.contactPerson);
                  NumberOfAliveSpecies = itemView.FindViewById&lt;TextView&gt;(Resource.Id.noOfAliveSpecies);
                  NumberOfDeadSpecies = itemView.FindViewById&lt;TextView&gt;(Resource.Id.noOfDeadSpecies);
                  DistanceOfTransporterToPIC = itemView.FindViewById&lt;TextView&gt;(Resource.Id.areaInKM);
                  ContactAddress = itemView.FindViewById&lt;TextView&gt;(Resource.Id.address);
                  ContactNumber = itemView.FindViewById&lt;TextView&gt;(Resource.Id.mobileNo);
                  mapIcon = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.mapIcon);
                  contactIcon = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.contactIcon);
                  phoneIcon = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.phoneIcon);
  
                  cardView = itemView.FindViewById&lt;CardView&gt;(Resource.Id.mainCardviewLayout);
                  MainLinearLayout = itemView.FindViewById&lt;LinearLayout&gt;(Resource.Id.MainLinearLayout);
                  ContactNumber.Click += (sender, e) =&gt; phoneClickListener(base.Position);
              }
          }
      }
  
  
  }
  </code></pre>