I'm working on a xamarin.forms recreation of a xamarin.android project and am having a problem with item formatting in list views.
The image on the left is how it should look but when i scroll down, the image on the right shows how a 'random' item gets the blue background from the grouped header cell.
If i continue to scroll up and down the chaos increases and more and more items succumb to the lure of their blue brethren.
I had a similar issue in xamarin.android where the headers would be bold and make random items bold as well, not sure if this is helpful or not. Providing code for completeness.
Custom List View
public class GroupedListView<K,T>
{
public GroupedListView(ObservableCollection<Grouping<K,T>> items)
{
if (Device.OS != TargetPlatform.WinPhone)
GroupHeaderTemplate = new DataTemplate(typeof(GroupedHeaderCell));
ItemsSource = items;
IsGroupingEnabled = true;
HasUnevenRows = true;
ItemTemplate = new DataTemplate(typeof(ListItemRegularCell));
}
}
Grouping Class
public class Grouping<K, T> : ObservableCollection<T>
{
public K Key { get; private set; }
public Grouping(K key, IEnumerable<T> items)
{
Key = key;
foreach (var item in items)
this.Items.Add(item);
}
}
Grouped Header Cell
public class GroupedHeaderCell : ViewCell
{
public GroupedHeaderCell()
{
this.Height = 50;
var title = new Label
{
Font = Font.SystemFontOfSize(NamedSize.Small, FontAttributes.Bold),
TextColor = Color.White,
VerticalOptions = LayoutOptions.Center
};
title.SetBinding(Label.TextProperty, "Key");
View = new StackLayout
{
HorizontalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 50,
BackgroundColor = Colours.LightBlue,
Padding = 5,
Orientation = StackOrientation.Horizontal,
Children = { title }
};
}
}
The forms project is being run on an android phone, i've tried it on several phones of varying brands and makes but unfortunately cannot currently try it on iOS.
If anyone has any suggestions for how to deal with this or has had similar experiences i would be eternally grateful for any advice.