1
votes

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?

1
So you mean to say you have 3 colours. 2 for the alternating list items and 1 if any of them is selected?@ShaneAkash Amin
@AkashAmin Yes, thats correct. The iOS part works but I can't find a method similar to 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 selectedJohn
Perhaps this SO answer can point you in the right directionNicolai Henriksen

1 Answers

1
votes

For Android, in the styles.xml in the custom theme definition i have set:

<item name="android:colorFocusedHighlight">@color/color_coloredbackground</item>
<item name="android:colorActivatedHighlight">@color/color_coloredbackground</item>
<item name="android:activatedBackgroundIndicator">@color/color_coloredbackground</item>

And this solves my selected background color in any listview. I guess that could help you.