How to animate xamarin listview items?
ListView allows you to animate the listview items by overriding the ListViewItem OnItemAppearing virtual method which is raised when the listviewitem appearing in the view.
This can be achieved by extending the ItemGenerator class, applying animation for the listview items using the OnItemAppearing method and aborting animation for the collapsed item using the PropertyChanged event.
public class Behaviours: Behavior<SfListView> { private SfListView listView; protected override void OnAttachedTo(BindableObject bindable) { listView = bindable as SfListView; listView.ItemGenerator = new ItemGenerator(listView); base.OnAttachedTo(bindable); } } |
Extending the ItemGenerator class
public class ItemGeneratorExt : ItemGenerator { public SfListView ListView { get; set; } public ItemGeneratorExt(SfListView listview): base(listview) { ListView = listview; } protected override ListViewItem OnCreateListViewItem(int itemIndex, ItemType type, object data = null) { if (type == ItemType.Record) return new ListViewItemExt(ListView); return base.OnCreateListViewItem(itemIndex, type, data); } } |
Extending ListVeiwItem class to animate and abort the listview items animation.
public class ListViewItemExt : ListViewItem { private SfListView _listView;
public ListViewItemExt(SfListView listView) { _listView = listView; } protected override void OnItemAppearing() { this.Opacity = 0; this.FadeTo(1, 400, Easing.SinInOut); base.OnItemAppearing(); } } |
Click here to download the sample.