I am having a hard time figuring what's going on with my animation.
Viewmodel is composed of an ObservableCollection and every item contains a child ObservableCollection.
Parents collection is bound to a BindableLayout. The ItemTemplate of that layout contains a Listview to display child's elements.
<StackLayout BindableLayout.ItemsSource ="{Binding Parents}">
<BindableLayout.ItemTemplate>
<DataTemplate x:DataType="models:ParentRows">
<StackLayout>
<Grid BackgroundColor="White" >
<!-- Some bindable content there -->
</Grid>
<ListView ItemsSource="{Binding Childs}" CachingStrategy="RecycleElementAndDataTemplate" RowHeight="50">
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:ChildRows">
<ViewCell>
<StackLayout BackgroundColor="White">
<controls:AnimatedGrid Refresh="{Binding Animation}"
<!-- Some bindable content there -->
</controls:AnimatedGrid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
I am using an AnimatedGrid on the child listview, this control is inherinting from Grid. It has an extra BindableProperty nammed Refresh and an Animation code that gets called whenever Refresh property changes.
private async void AnimateItem()
{
await Device.InvokeOnMainThreadAsync(() =>
{
this.RotateTo(360,500);
});
}
Everything works fine until i start filtering the list. Once i filter the list, subsequent call of AnimateItem will have no effect. To be more precise, if parent item got removed from list, and then added again, childs of this parent will never animate again. Filtering the List consist of Removing/Inserting parents to the observable collection (myCollection.Remove(item), myCollection.Insert(index, item), using Collection methods from framework).
This does not seems to be an observable collection binding issue, as values inside parent and childs collection still update perfectly find. Changing CachingStrategy also have no impact on the issue.
I found that, if i replace the ListView control by a CollectionView, the problem disappear. However, i realy want to find a solution that would alow me to keep the listview control as switching to CollectionView would introduce to many other undesirable effect.
Edit 22/02/2022 : I made a sample project to reproduce the issue on github.
- Basicaly, you can click the "Rotate Random" multiple time to make random child spin.
- Once you click the "Remove and add 2 parent", you can see that the removed/reinserted items does not rotate anymore.
Edit 15/03/2022 : I am still not able to figure what's wrong. However, for test purpose, i added in the control constructor, a task.delay followed by an animation call, and this call is working on filtered items. That's beyond my understanding.