{"record":{"id":"f0fd955325076e3d","repo":"dotnet/maui","slug":"view-for-cell-must-implement-nameof-inativeelemen","errorCode":null,"errorMessage":"View for cell must implement {nameof(INativeElementView)} to enable recycling.","messagePattern":"View for cell must implement (.+?) to enable recycling\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs","lineNumber":256,"sourceCode":"\t\t\tvar cellIsBeingReused = false;\n\t\t\tvar layout = convertView as Handlers.Compatibility.ConditionalFocusLayout;\n\t\t\tif (layout != null)\n\t\t\t{\n\t\t\t\tcellIsBeingReused = true;\n\t\t\t\tconvertView = layout.GetChildAt(0);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tlayout = new Handlers.Compatibility.ConditionalFocusLayout(_context) { Orientation = Orientation.Vertical };\n\t\t\t\t_layoutsCreated.Add(layout);\n\t\t\t}\n\n\t\t\tif (((cachingStrategy & ListViewCachingStrategy.RecycleElement) != 0) && convertView != null)\n\t\t\t{\n\t\t\t\tvar boxedCell = convertView as INativeElementView;\n\t\t\t\tif (boxedCell == null)\n\t\t\t\t{\n\t\t\t\t\tthrow new InvalidOperationException($\"View for cell must implement {nameof(INativeElementView)} to enable recycling.\");\n\t\t\t\t}\n\t\t\t\tcell = (Cell)boxedCell.Element;\n\n\t\t\t\tICellController cellController = cell;\n\t\t\t\tcellController.SendDisappearing();\n\n\t\t\t\tint row = position;\n\t\t\t\tvar group = 0;\n\t\t\t\tvar templatedItems = TemplatedItemsView.TemplatedItems;\n\t\t\t\tif (_listView.IsGroupingEnabled)\n\t\t\t\t\tgroup = templatedItems.GetGroupIndexFromGlobal(position, out row);\n\n\t\t\t\tvar templatedList = templatedItems.GetGroup(group);\n\n\t\t\t\tif (_listView.IsGroupingEnabled)\n\t\t\t\t{\n\t\t\t\t\tif (row == 0)\n\t\t\t\t\t\ttemplatedList.UpdateHeader(cell, group);","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/Renderers/ListViewAdapter.cs#L238-L274","documentation":"ListViewAdapter.GetView, when the ListView uses RecycleElement caching and a convertView is provided (i.e. Android is recycling), requires convertView to implement INativeElementView so it can recover the Cell via boxedCell.Element. If the recycled view is a plain view (e.g. a custom ViewCell whose renderer does not implement INativeElementView), recycling cannot proceed and an InvalidOperationException is thrown.","triggerScenarios":"ListView with CachingStrategy=RecycleElement whose ViewCell renders a custom view that does not implement INativeElementView, and Android attempts to recycle that view (convertView != null).","commonSituations":"Custom ViewCell with a renderer returning a native Android view that skips the INativeElementView contract; mixing a hand-rolled cell renderer with RecycleElement; upgrading a RetainElement-style custom cell to a recycled ListView without implementing the interface.","solutions":["Have the custom view/renderer implement INativeElementView (expose Element).","If you cannot implement the interface, switch the ListView to CachingStrategy=RetainElement (at a memory cost).","Use the standard Cell subclasses whose renderers already implement INativeElementView."],"exampleFix":"// before — custom view without the interface\npublic class MyCustomView : LinearLayout { /* no INativeElementView */ }\n// after\npublic class MyCustomView : LinearLayout, INativeElementView\n{\n    public Element Element { get; set; }\n}","handlingStrategy":"validation","validationCode":"if (listView.CachingStrategy == ListViewCachingStrategy.RecycleElement)\n{\n    foreach (var vc in listView.TemplatedItems)\n        if (vc?.View is not null && vc.Renderer is Android.Views.View av && av is not INativeElementView)\n            listView.CachingStrategy = ListViewCachingStrategy.RetainElement; // or fix the renderer\n}","typeGuard":"static bool SupportsRecycle(ViewCell cell)\n{\n    var renderer = Platform.GetRenderer(cell.View);\n    return renderer?.GetView?.(cell.View) is INativeElementView; // pseudo\n}\n// simpler: ensure the custom view class implements INativeElementView","tryCatchPattern":"try { /* build ListView */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"INativeElementView\"))\n{ listView.CachingStrategy = ListViewCachingStrategy.RetainElement; }","preventionTips":["Make custom cell views implement INativeElementView if you use RecycleElement.","Validate cell renderers against the interface before enabling recycling.","Fall back to RetainElement only as a documented trade-off (higher memory)."],"tags":["android","listview","recycle","viewcell","caching-strategy","maui-compat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}