locked
Problem with selected cells when using RecycleElement caching strategy on ListView RRS feed

  • Question

  • User37589 posted

    Hi guys,

    I have a ListViewwith caching strategy set to RecycleElement, where each cell has a certain number of context actions implemented with MenuItems. In Android, when the user long presses on a cell the actions are shown and the background color of the selected cell is changed. The problem is that if the user starts scrolling down the list while a cell is selected, after the selected cell goes out of the screen, another cell at the bottom appears with the background color changed, as if it was selected, and this happens repeatedly (if one has enough list items), and this is confusing. The issue is not present when using the RetainElement caching strategy, to this should be a problem related to the recycling of cells.

    Any ideas about to solve this issue?

    Monday, December 14, 2015 9:09 AM

Answers

  • User181 posted

    Well that's definitely a bug. Sorry for the misunderstanding. You should report that bug here: http://bugzilla.xamarin.com

    You should attach your project, but before you do you need to make it smaller. To do that just do a clean (or delete all the bin and obj directories) and then delete all of the subdirectories inside the packages directory. Once you do that the whole thing should be well under a MB, and you can attach a zip to the bug report.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, December 17, 2015 5:34 PM

All replies

  • User181 posted

    Make sure that the state of your cells is entirely dependent on the BindingContext. Since cells are reused you can't store any state in them directly. For instance, if you are manually setting some property on the cell when it becomes selected to change its appearance then that state will still be present when that cell is reused for some other row. Instead you have to keep the state in the view model (the object in the ItemsSource collection) and use bindings to update the cell's appearance.

    If you think you're doing this right then make a small test app that demonstrates the problem and attach it here.

    Monday, December 14, 2015 5:58 PM
  • User37589 posted

    I am aware of that, the problem is that on Android the background color of a cell that has context actions active is defined on Android theme. And in Xamarin it cannot be detected when context are shown and hidden, therefore you cannot really set the background manually, you have to rely on the theme.

    Tuesday, December 15, 2015 10:43 AM
  • User181 posted

    I don't understand what any of that has to do with what I said. If you use RecycleElement you must define the appearance of your cell purely based on the BindingContext. If you have a reason you can't possibly do that then you just can't use RecycleElement.

    Tuesday, December 15, 2015 4:45 PM
  • User37589 posted

    The problem is that I am not setting the background color of the selected cell myself, but it is automatically set when the user long presses on an cell that has context action, so I cannot define it manually based on the binding context. I've attached a simple test project to clarify the problem. In order to see the issue, long press on whatever element of the list (in order it to show the context action) then try scrolling. You'll see that the background of the selected cell will appear also on other cells

    Thursday, December 17, 2015 9:24 AM
  • User181 posted

    Well that's definitely a bug. Sorry for the misunderstanding. You should report that bug here: http://bugzilla.xamarin.com

    You should attach your project, but before you do you need to make it smaller. To do that just do a clean (or delete all the bin and obj directories) and then delete all of the subdirectories inside the packages directory. Once you do that the whole thing should be well under a MB, and you can attach a zip to the bug report.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, December 17, 2015 5:34 PM
  • User93359 posted

    I have this same issue only on Android starting with Marshmallow. I see the issue when I set the selected listview item with CachingStrategy="RecycleElement". No issues on IOS and no issue when CachingStrategy is retain.

    I have a button in the row defined by the data template and when it is clicked I set the selected listview item.

    var btnSender = (Button)sender;
    var SelectedDocument = btnSender.BindingContext as CabinetApp.ViewModels.DocumentViewModel; this.lstDocuments.SelectedItem = SelectedDocument;

    Now if you start scrolling you see a selected row over and over. Tested with Forms Version 2.0.1.6492-pre1

    Monday, January 4, 2016 10:08 PM
  • User233049 posted

    how to enable caching strategy in xaml ?

    Friday, June 24, 2016 4:28 PM
  • User35026 posted

    @asfend , just create a Custom ListView like that :

    public class OptimizedListView : ListView { public OptimizedListView() : base(ListViewCachingStrategy.RecycleElement) { } }

    Friday, August 26, 2016 8:06 AM