{
public class ViewCellRenderer : CellRenderer
{
+ readonly Dictionary<EvasObject, ViewCell> _cacheCandidate = new Dictionary<EvasObject, ViewCell>();
public ViewCellRenderer() : base("full")
{
MainContentPart = "elm.swallow.content";
protected string MainContentPart { get; set; }
+ protected override EvasObject OnReusableContent(Cell cell, string part, EvasObject old)
+ {
+ if (_cacheCandidate.ContainsKey(old))
+ {
+ var viewCell = _cacheCandidate[old];
+ var widget = (old as Widget);
+ if (widget != null)
+ widget.IsEnabled = true;
+ viewCell.BindingContext = cell.BindingContext;
+ return old;
+ }
+ return null;
+ }
+
protected override EvasObject OnGetContent(Cell cell, string part)
{
if (part == MainContentPart)
{
- var viewCell = cell as ViewCell;
- if (viewCell != null)
- {
- var renderer = Platform.GetOrCreateRenderer(viewCell.View);
- int height = (int)viewCell.RenderHeight;
- height = height <= 0 ? FindCellContentHeight(viewCell) : height;
+ var viewCell = (ViewCell)cell;
- renderer.NativeView.MinimumHeight = height;
- return renderer.NativeView;
+ var listView = viewCell?.RealParent as ListView;
+
+ // It is a condition for reusable the cell
+ if (listView != null &&
+ listView.HasUnevenRows == false &&
+ !(listView.ItemTemplate is DataTemplateSelector))
+ {
+ return CreateReusableContent(viewCell);
}
- return null;
+
+ var renderer = Platform.GetOrCreateRenderer(viewCell.View);
+ int height = (int)viewCell.RenderHeight;
+ height = height > 0 ? height : FindCellContentHeight(viewCell);
+
+ renderer.NativeView.MinimumHeight = height;
+ return renderer.NativeView;
}
return null;
}
}
return base.OnCellPropertyChanged(cell, property, realizedView);
}
+
+ EvasObject CreateReusableContent(ViewCell viewCell)
+ {
+ var listView = viewCell.RealParent as ListView;
+ ViewCell duplicatedCell = (ViewCell)listView.ItemTemplate.CreateContent();
+ duplicatedCell.BindingContext = viewCell.BindingContext;
+ duplicatedCell.Parent = listView;
+
+ var renderer = Platform.GetOrCreateRenderer(duplicatedCell.View);
+ int height = (int)duplicatedCell.RenderHeight;
+ height = height > 0 ? height : FindCellContentHeight(duplicatedCell);
+ renderer.NativeView.MinimumHeight = height;
+
+ _cacheCandidate[renderer.NativeView] = duplicatedCell;
+ renderer.NativeView.Deleted += (sender, e) =>
+ {
+ _cacheCandidate.Remove((EvasObject)sender);
+ };
+
+ return renderer.NativeView;
+ }
}
}