0

I cant find solution about this orange color? Do i need to write a renderer or can change from resources in Android and IOS?

Rajzer
  • 975
  • 4
  • 16
  • Set the SelectionMode of your ListView to None!!! – FreakyAli Feb 11 '20 at 14:07
  • Does this answer your question? [Xamarin.Forms ListView: Set the highlight color of a tapped item](https://stackoverflow.com/questions/25885238/xamarin-forms-listview-set-the-highlight-color-of-a-tapped-item) – Ricardo Dias Morais Feb 11 '20 at 14:26

1 Answers1

1

Yes,if you want to change ListView selecteditem background color, need to use custom render to do this in Xamarin.Forms.

In the PCL, create a class name is ExtendedViewCell which should inherit any ViewCell.

public class ExtendedViewCell : ViewCell
{
    public static readonly BindableProperty SelectedBackgroundColorProperty =
        BindableProperty.Create("SelectedBackgroundColor",
                                typeof(Color),
                                typeof(ExtendedViewCell),
                                Color.Default);

    public Color SelectedBackgroundColor
    {
        get { return (Color)GetValue(SelectedBackgroundColorProperty); }
        set { SetValue(SelectedBackgroundColorProperty, value); }
    }
}

In Android project, create a class name as ExtendedViewCellRenderer and make sure to add renderer registration for our ExtendedViewCell class above the namespace.

[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
namespace demo3.Droid
{
public class ExtendedViewCellRenderer : ViewCellRenderer
{

    private Android.Views.View _cellCore;
    private Drawable _unselectedBackground;
    private bool _selected;

    protected override Android.Views.View GetCellCore(Cell item,
                                                      Android.Views.View convertView,
                                                      ViewGroup parent,
                                                      Context context)
    {
        _cellCore = base.GetCellCore(item, convertView, parent, context);

        _selected = false;
        _unselectedBackground = _cellCore.Background;

        return _cellCore;
    }

    protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        base.OnCellPropertyChanged(sender, args);

        if (args.PropertyName == "IsSelected")
        {
            _selected = !_selected;

            if (_selected)
            {
                var extendedViewCell = sender as ExtendedViewCell;
                _cellCore.SetBackgroundColor(extendedViewCell.SelectedBackgroundColor.ToAndroid());
            }
            else
            {
                _cellCore.SetBackground(_unselectedBackground);
            }
        }
    }
}
}

Then you can set color for listview SelectedBackgroundColor.

  <ListView ItemsSource="{Binding students}">
            <ListView.ItemTemplate>
                <DataTemplate>

                    <local:ExtendedViewCell SelectedBackgroundColor="White">
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Username}" TextColor="Yellow" />
                            <Label Text="{Binding Age}" TextColor="Blue" />
                        </StackLayout>
                    </local:ExtendedViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

More detail info and steps in ios platform, you can take a look:

https://blog.wislon.io/posts/2017/04/11/xamforms-listview-selected-colour

Update:

In ios project, create a class name as ExtendedViewCellRenderer and make sure to add renderer registration for our ExtendedViewCell class above the namespace.

 [assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
 namespace xamformsdemo.iOS
 {
    public class ExtendedViewCellRenderer : ViewCellRenderer
{
    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        var cell = base.GetCell(item, reusableCell, tv);
        var view = item as ExtendedViewCell;
        cell.SelectedBackgroundView = new UIView
        {
            BackgroundColor = view.SelectedBackgroundColor.ToUIColor(),
        };

        return cell;
    }

}
 }
Cherry Bu - MSFT
  • 7,773
  • 1
  • 5
  • 12
  • and solution for IOS? – Rajzer Feb 12 '20 at 08:14
  • Please look at my update on reply, and if you have other issue, I suggest you can take a look this article:[Xamarin Forms - Change a ListView's Selected Item Colour](https://blog.wislon.io/posts/2017/04/11/xamforms-listview-selected-colour) – Cherry Bu - MSFT Feb 12 '20 at 08:26
  • @Rajzer yes, the Microsoft articles about custom render also suggest we to do like this:[Xamarin.Forms Custom Renderers](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/) – Cherry Bu - MSFT Feb 12 '20 at 08:50
  • We can make custom renderer on all xamarin.forms components? – Rajzer Feb 12 '20 at 08:58
  • By this way, If I set CachingStrategy="RetainElement", the selected background color will have strange behavior. For example, I selected one item, the background is correct at that time. Then, If I scroll down and up the list, the background color will revert to the default color(Orange), and many other items' background color will be the color that I set in 'ExtendedViewCellRenderer'. – Xie Steven May 18 '21 at 10:05