I have a custom ListView and a custom ViewCell for a Xamarin Forms project with iOS and android. I created the custom listView so that my list could alternate colors (which is pretty much successful). Unfortunately when I created the custom ListView it stopped highlighting the selected cell so thats why I created the custom ViewCells.
I'm having trouble with the android side of things. I can change the color when the cell is selected. But I'm not sure how to change the color back once another cell is selected.
Here's my CustomList class ...
public class CustomList : ListView
{
public CustomList (){}
protected override void SetupContent(Cell content, int index)
{
base.SetupContent (content, index);
var currentCell = content as ViewCell;
currentCell.View.BackgroundColor = index % 2 == 0 ? Color.FromRgb (235, 235, 235) : Color.FromRgb (255, 255, 255);
}
}
Pretty straight forward, just sets the color based on the index. (Maybe theres something else I can add in here to handle the selected cell?)
Heres the CustomCell class :
public class CustomCell : ViewCell
{
public const String isSelectedProperty = "IsSelected";
public CustomCell ()
{
}
}
and in the iOS Renderer:
public class CustomCellRenderer : ViewCellRenderer
{
private UIView view;
public CustomCellRenderer ()
{
}
public override UITableViewCell GetCell (Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell (item, reusableCell, tv);
if (view == null) {
view = new UIView (cell.SelectedBackgroundView.Bounds);
view.Layer.BackgroundColor = UIColor.FromRGB (128, 204,255).CGColor;
}
cell.SelectedBackgroundView = view;
return cell;
}
}
and the android renderer:
public class CustomCellRenderer : ViewCellRenderer
{
public CustomCellRenderer ()
{
}
protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
{
var cell = base.GetCellCore (item, convertView, parent, context);
return cell;
}
protected override void OnCellPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnCellPropertyChanged (sender, e);
var customCell = sender as CustomCell;
if (e.PropertyName == CustomCell.isSelectedProperty) {
customCell.View.BackgroundColor = Color.FromRgb (128, 204, 255);
}
}
}
So the iOS part works. And the android part 'works' as in it selects the color of the cell but the color stays. I want only the selected cell to have a different background color. Any ideas?
cell.SelectedBackgroundView
for android. What I have for android works also but it just changes the color of the cell all together and doesn't change back when its not selected – John