Unexpected behavior when selecting listview items with changed background color only on iOS

KybeKybe Member

Dear all,

I'm currently developing an app with Xamarin.Forms where i got an very strange behavior when I select listview items.
One feature of this app should be a view cell with a non default background color when selected.

I tried many ways (*not tested on a real apple device): dataTriggers, stackLayout with backgroundColor binding, custom viewCells

The last one successed on android but on iOS the following is happening:
Example: 3 Items ('1', '2', '3')

Select: 1

  • Nothing happens
  • Select: 3

  • 1 Selected

Select: 2

  • 3 Selected

so on .....

Code:

  [assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
  namespace 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;
      } 


 [assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
 namespace Android
 {
   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);
       }
     }
   }
}


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); }
        }
}

<ListView VerticalOptions="FillAndExpand"  HorizontalOptions="FillAndExpand"  BackgroundColor="Black"  ItemsSource="{Binding .}" RowHeight="52">
<ListView.ItemTemplate>
            <DataTemplate>
                <customControls:ExtendedViewCell SelectedBackgroundColor="Teal">
          <ViewCell.View>
            <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Orientation="Vertical" Padding="4" Spacing="8">
                <Label TextColor="White" Text="{Binding Name}"/>
            </StackLayout>
          </ViewCell.View>
        </customControls:ExtendedViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

BindingContext = public ObservableCollection<Model> MyModels { get; set; }

Does anyone know whats wrong here?

All nuget versions are up to date.
Tried on iPhone 6s Plus, iPhone 8 Plus (Simulator).

Thank you

Sign In or Register to comment.