Implement Reusable ViewCell
authorSeungkeun Lee <sngn.lee@samsung.com>
Wed, 21 Dec 2016 00:55:45 +0000 (09:55 +0900)
committerKangho Hur <kangho.hur@samsung.com>
Mon, 10 Jul 2017 02:11:15 +0000 (11:11 +0900)
 - Reusable feature was applied when HasUneventRows is false and the ItemTemplate is not created from the DataTEmplateSelector
 - It means all items has same looks

Change-Id: Iadc1262ff4dcf001b1c767fb479851efa6632c44

Xamarin.Forms.Platform.Tizen/Cells/ViewCellRenderer.cs

index 69065c8..6328778 100644 (file)
@@ -5,6 +5,7 @@ namespace Xamarin.Forms.Platform.Tizen
 {
        public class ViewCellRenderer : CellRenderer
        {
+               readonly Dictionary<EvasObject, ViewCell> _cacheCandidate = new Dictionary<EvasObject, ViewCell>();
                public ViewCellRenderer() : base("full")
                {
                        MainContentPart = "elm.swallow.content";
@@ -12,21 +13,42 @@ namespace Xamarin.Forms.Platform.Tizen
 
                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;
                }
@@ -39,5 +61,26 @@ namespace Xamarin.Forms.Platform.Tizen
                        }
                        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;
+               }
        }
 }