Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms - Force ListView layout to redraw

I have a ListView with HasUnevenRows = true, where the content of each cell is variant to begin with, but the content can also change on the fly (through clicking of a button in the cell). When the ListView initially loads, the system must perform a calculation in order to correctly determine and draw the heights of each cell based on their content.

But when I update the content after the initial load, this same calculation is apparently not done, because the height of the cell does not change -- until I've scrolled that item out of view and then back into view.

How can I force this same calculation and redraw manually, without completely refreshing the ListView??

I tried wrapping the ListView in a Frame and calling .ForceLayout() on the frame, but no luck.

like image 676
jbyrd Avatar asked May 24 '16 12:05

jbyrd


People also ask

How to refresh the ListView in Xamarin forms?

Xamarin Forms ListView control has the ability to allow the user to pull down from the top of the ListView to trigger a refresh command. When the user triggers a PullToRefresh the Command will be invoked the Refreshed event.

How do I refresh view in xamarin?

Create a RefreshView RefreshView refreshView = new RefreshView(); ICommand refreshCommand = new Command(() => { // IsRefreshing is true // Refresh data here refreshView. IsRefreshing = false; }); refreshView.


2 Answers

The height of the cells aren't recalculated by default because it is an expensive process. If you want to force a re-size you need to call ForceUpdateSize() on the cell.

NOTE: This does not work on older versions of Xamarin Forms. It was added in 2.0.0.0, but try updating to at least 2.2.0.31

like image 137
Christine Avatar answered Sep 28 '22 02:09

Christine


Set your CachingStrategy="RetainElement", it's expensive, but will force an update a lot more. If your list isn't too long, you won't notice.

I've noticed that if a cell is visible, even though bound to an obserablecollection, that cell doesn't update until I scroll up then down.

like image 35
Ian Vink Avatar answered Sep 28 '22 03:09

Ian Vink