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.

https://developer.xamarin.com/samples/monodroid/android5.0/RecyclerViewer/

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 stackoverflow.com. I couldn't quite apply their JAVA solutions to XAMARIN.

https://stackoverflow.com/questions/32427889/checkbox-in-recyclerview-keeps-on-checking-different-items

Also, here is a similar ListView related issue -

https://forums.xamarin.com/discussion/23637/android-checkbox-listview-checkboxes-automcatically-check-uncheck-upon-scrolling

Here are the modifications I made to the sample -

1) Added a simple checkbox to the PhotoCardView layout

<CheckBox
    android:text="CheckBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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;
                }
                else
                {
                     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.

Thanks..!!

Best Answer

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

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

    view.ioClickListener()
    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 ✭✭

    @ChristopheBERNARD...

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

Sign In or Register to comment.