Wenfengcheng / xamarin-notes

xamarin journal
MIT License
3 stars 0 forks source link

FFImageLoading in ListView with ListViewCachingStrategy.RecycleElement enabled #32

Closed Wenfengcheng closed 5 years ago

Wenfengcheng commented 5 years ago

If you use CachedImage views inside ViewCells used by ListViews with ListViewCachingStrategy.RecycleElement enabled, don't rely on bindings for setting the Source property (they are so slow!) - please use ViewCell's OnBindingContextChanged override of a ViewCell implementation to update it, avoiding loading images to wrong views. Example:

public class MyCustomCell : ViewCell
{
  readonly CachedImage cachedImage = null;

  public MyCustomCell()
  {
      cachedImage = new CachedImage();
      View = cachedImage;
  }

  protected override void OnBindingContextChanged()
  {
      // you can also put cachedImage.Source = null; here to prevent showing old images occasionally
      cachedImage.Source = null;
      var item = BindingContext as Item;

      if (item == null)
      {
          return;
      }

      cachedImage.Source = item.ImageUrl;

      base.OnBindingContextChanged();
  }
}

https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/performance/#RecycleElement