ListView Item-View cleanup

1 Answer 28 Views
ListView
Nico
Top achievements
Rank 1
Nico asked on 31 Jan 2024, 01:56 PM

I couldn't figure out how to clean up ListView items after changing the item-source or leaving a page.

Let's take your TemplateCell Example from

https://docs.telerik.com/devtools/maui/controls/listview/cells/template-cell

If i use a custom view instead of the grid it doesn't get a unloaded event or disposed or something else.
This prevents me to do a native view cleanup in my custom view.  Consequently i can't disconnect Handlers resulting in memory leaks.

Am I missing something here? Something in the documentation?
Is there an example?

Nico
Top achievements
Rank 1
commented on 01 Feb 2024, 10:34 AM

I've created a minimal repo example showing whats not working:

Using the unloaded event in the custom view itself to cleanup (ListViewContent  in the repo):

https://github.com/baaaaif/TelerikListViewCleanup

Doing so, when navigating to the second page and back - the list view items are broken

1 Answer, 1 is accepted

Sort by
0
Didi
Telerik team
answered on 02 Feb 2024, 12:14 PM

Hi Nico,

If you want to remove the ListView items, you can set the source to null/clear the Items collection. And then set the items again on the OnAppearing method. 
I am not sure why you use the Unloaded event of the ContentView. 

Another approach is to create a custom cell that inherits from Telerik.Maui.Controls.Compatibility.DataControls.ListView.ListViewTemplateCell and override the OnBindingContextChanged. I have attached this approach in the sample app. 

Please check the code and the provided solutions and let me know if you have any additional questions. I have also left comments in the code for convenience.

 

Nico
Top achievements
Rank 1
commented on 17 Apr 2024, 01:27 PM

Hi Didi,

i came back to this because our QS reported leaks.

I tried the inherited ListViewTemplateCell approach, but unfortunately onBindingContextChanged and onParentSet/onHandlerChanged etc isn't called on Android. And i can't find a way to get the parent ListView from the cell too (works on Windows).

The view in the cell is complex and i need to manually deep clean it up the entire visual tree down to avoid memory leaks.
So i need to know when and how to determine, when the cell is ready to be disposed (not recycled/unloaded for a later reuse)

Perfect would be a custom Cell inherited from ListViewTemplateCell which implements IDisposeable...

Didi
Telerik team
commented on 19 Apr 2024, 08:43 AM

Hi Nico,

You can create a custom cell that inherits from the ListViewTemplateCell. This is displayed in the project I attached previously. If you need further implementation for this cell, I can suggest you contact the professional services team

Tags
ListView
Asked by
Nico
Top achievements
Rank 1
Answers by
Didi
Telerik team
Share this question
or