Forum Xamarin.Android

Checkboxes in XAMARIN RecyclerView Sample...

HowdyHoHowdyHo USMember ✭✭
edited December 2018 in Xamarin.Android

I am modifying this official sample from XAMARIN Android for a RecyclerView.

I want to have a checkBox in every row where I can select the photos that I like. I have added a checkBox just below the captions. Now, whenever I select on a checkBox inside a row, several checkBoxes downstream appear as selected. For example, when I select a checkBox on the first photo and scroll down the RecyclerView, I see several photos that already have the checkBox selected. I believe its a well known issue and is primarily because of recycling of these views.

Here is a discussion on I couldn't quite apply their JAVA solutions to XAMARIN.

Also, here is a similar ListView related issue -

Here are the modifications I made to the sample -

1) Added a simple checkbox to the PhotoCardView layout

    android:id="@+id/myCheckBox" />

2) Created a two properties in the PhotoViewHolder -

public CheckBox MyCheckBox { get; set; }
public bool IsChecked { get; set; }

3) Did a FindViewById for the checkbox

 MyCheckBox = itemView.FindViewById<CheckBox>(Resource.Id.myCheckBox);

4) Created a click event for MyCheckBox

 MyCheckBox.Click += delegate

                if (MyCheckBox.Checked)
                    Console.WriteLine("I can get the adapter position here {0}", AdapterPosition);                    
                    IsChecked = true;
                     Console.WriteLine("I can get the adapter position here {0}", AdapterPosition);                 
                     IsChecked = false;

Question - How to stop this CheckBox repeating issue and make sure that the CheckBoxes do not appear as checked unless I actually click on them.

Any help is much appreciated.


Best Answer


  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello @HowdyHo
    i think your event is locate into custom adapter class ?
    If yes, perhpas try to inherid this class from

    Implement the interface and assign the checkbox to this event mycheckBox.SetOnClickListener(this);

    Never, try to add a checkbox into a custom adapter but perhaps try this and tell us

  • HowdyHoHowdyHo USMember ✭✭

    Works just beautifully. A very big Thank you.

  • HowdyHoHowdyHo USMember ✭✭


    Thanks for taking a shot at it..!!..

Sign In or Register to comment.