Home > Software design >  How to Animate ListView items in Xamarin Forms?
How to Animate ListView items in Xamarin Forms?

Time:11-02

I wanted to animate the list items after they are added from an API call. But only animate them when they first load in.

CodePudding user response:

To access the ViewCell you use the event Appearing on the ViewCell element. You can set a flag on the Scrolled event to prevent animations on subsequent Appearing triggers.

<ListView Scrolled="listView_Scrolled" ItemsSource="{Binding Items}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell Appearing="ViewCell_Appearing">
                [Content Here]
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
private bool scrolled = false;

...

void ViewCell_Appearing(object sender, EventArgs e)
{
    if (!scrolled)
    {
        ViewCell cell = (ViewCell)sender;
        CustomAnimation(cell);
    }
}

void listView_Scrolled(object sender, ScrolledEventArgs e)
{
    scrolled = true;
}
  • Related