Bug with ListView

CristianoInsola.8408CristianoInsola.8408 Cristiano InsolaUSMember ✭✭
edited December 2016 in Xamarin.Forms

If I remove from an ObservableCollection list bound into a ListView the TranslationX property of the deleted item is applied to the next item. For example if I delete the item (2) the TranslationX of the old second item is applied to the new item 2 (which was the 3rd).
My example:

<StackLayout>
        <Button Clicked="removeitem" Text="remove first element"></Button>
        <ListView ItemsSource="{Binding items}" x:Name="listView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <c:GestureListItem />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

Code behind to add some data:

    public partial class MainPage : ContentPage
    {
        public ObservableCollection<string> items { get; set; }
        public MainPage()
        {
            items = new ObservableCollection<string>();
            items.Add("first");
            items.Add("second");
            items.Add("third");
            items.Add("forth");
            items.Add("fifth");
            InitializeComponent();
            listView.BindingContext = this;
        }

        private void removeitem(object sender, EventArgs e)
        {
            items.Remove(items[0]);
        }
    }

In my ViewCell version i added a BackgroundColor binding to show that this property works. The problem effects only TranslationX:

<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:swipe="clr-namespace:Xamtools"
      x:Class="Xamtools.GestureListItem">
    <BoxView x:Name="boxview" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" TranslationX="{Binding TransX}" BackgroundColor="{Binding BackgroundColor}" />
</ViewCell>

The code behind:

    public partial class GestureListItem : ViewCell
    {
        private static Random r = new Random();
        public GestureListItem()
        {
            InitializeComponent();
            boxview.BindingContext = new Context
            {
                TransX = r.Next(10, 350),
                BackgroundColor = Color.FromRgb(r.Next(250), r.Next(150), r.Next(250))
            };
        }
    }

    public class Context : INotifyPropertyChanged
    {
        private double _transx = 0;
        private Color _color;

        public event PropertyChangedEventHandler PropertyChanged;

        public double TransX { get { return _transx; } set { if (value != _transx) { _transx = value; OnPropertyChanged("TransX"); } } }
        public Color BackgroundColor { get { return _color; } set { if (value != _color) { _color = value; OnPropertyChanged("BackgroundColor"); } } }

        private void OnPropertyChanged(string v)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
        }
    }

Anyway, the problem occurs also if you don't use databinding and set your translationX via code.
What have I to do?

Posts

  • AlessandroCaliaroAlessandroCaliaro Alessandro Caliaro ITMember ✭✭✭✭✭

    If you think this is a bug , is better to open a bug on bugzilla. Attach a full repo project so xamarin team can verify easily the problem

  • ClintStLaurentClintStLaurent Clint StLaurent USUniversity ✭✭✭✭✭
    edited December 2016

    @CristianoInsola.8408 said:

    For example if I delete the item (2) the TranslationX of the old second item is applied to the new item 2 (which was the 3rd).
    My example:
    ```
    items.Add("first");
    items.Add("second");
    items.Add("third");
    items.Add("forth");
    items.Add("fifth");
    InitializeComponent();
    listView.BindingContext = this;

    Hmmm... are you sure of your results versus your expectations. Because reading your comment and your code I'm almost lead to beleive you think collections start at 1. In C# collections are zero-indexed.
    [0] = First
    [1] = Second
    [2] = third

    So if you do what you are really saying "delete item (2)" you are really deleting the third item. If you already knew that, then cool. But if you didn't, it almost explains what you're describing.

    If nothing else, changing your naming while testing can avoid confusion

    items.Add("zero-th");
     items.Add("1st");
     items.Add("2nd");
     items.Add("3rd");
    
  • CristianoInsola.8408CristianoInsola.8408 Cristiano Insola USMember ✭✭

    The array I posted above is just an example. The problem is that while background color are preserved after deletion, translationX isn't

  • AdrianKnightAdrianKnight Adrian Knight USMember ✭✭✭✭
  • CristianoInsola.8408CristianoInsola.8408 Cristiano Insola USMember ✭✭
    edited December 2016

    It's not the same but it looks like it's similar. My problem appears when I delete an element, not on scrolling.

    mumble, nice too see the bug is there since 2 months without replies XD

  • AdrianKnightAdrianKnight Adrian Knight USMember ✭✭✭✭

    @CristianoInsola.8408 I submitted a potential fix for 44690 (https://github.com/xamarin/Xamarin.Forms/pull/637). You can submit a bug with a minimal repro so it can be investigated if your issue is different.

  • CristianoInsola.8408CristianoInsola.8408 Cristiano Insola USMember ✭✭

    Great, Ill wait the next release :)
    Anyway i submitted the bug here: https://bugzilla.xamarin.com/show_bug.cgi?id=49658

  • CristianoInsola.8408CristianoInsola.8408 Cristiano Insola USMember ✭✭

    I saw your fix has been refused. what should I do now?

Sign In or Register to comment.