Xamarin.Forms - Force ListView layout to redraw
Solution 1
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
Solution 2
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.
Solution 3
I've had terrible luck getting native controls to redraw. The one thing that has worked for me has been in a custom renderer: ((IVisualElementController)Element).NativeSizeChanged()
jbyrd
Updated on June 14, 2022Comments
-
jbyrd almost 2 years
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. -
jbyrd almost 8 yearsI don't want to completely refresh the list, because I'm in a particular state of an item that I need to preserve.
-
jbyrd almost 8 yearsI don't want to refresh the data, I only want to redraw the list in its current state.
-
Artem Zelinskiy almost 8 yearsIt will not refresh it. It will just recalculate row/list heights
-
Mr.Koçak almost 8 yearsDo you use a DataTemplate for your listview?
-
jbyrd almost 8 yearsYes, I have a separate file for the DataTemplate.
-
jbyrd almost 8 yearsI just tried it, but it has no effect. I added a "refresh" button with a
.Clicked
event that callsmyListView.BeginRefresh()
andmyListView.EndRefresh();
No luck. In the clicked even, I even called amyListView.TranslateX = 100;
to make sure it was affecting the list correctly, and it was - that worked. But BeginRefresh and EndRefresh didn't do anything. -
Mr.Koçak almost 8 yearsI know that when you delete something from your list and your itemsource is observableCollection it works, it upgrades the view.. but i don't know if it will work for you. And i also dont know if you can have different sizes per row in a listview.
-
Mr.Koçak almost 8 yearsI think in your datatemplate you should have something like if(condition==true) layout.Add(newLabel) else layout.remove(newLabel) to get condition value of your object you have to use databinding
-
jbyrd almost 8 years@MrKocak - what does that have to do with dynamically changing the height? Sorry, I'm not following.
-
jbyrd almost 8 yearsInteresting - I just tried that, but my application crashed on calling that command. I created a button inside the cell with a
.Clicked
event callingForceUpdateSize()
. -
jbyrd almost 8 years
ForceUpdateSize()
, haha - just that. -
jbyrd almost 8 yearsSo.....turns out my Xamarin.Forms version was the problem. I was on 2.1.0.6526. Once I upgraded to 2.2.0.31, it worked!!
-
Scott Nimrod almost 5 yearsI used CachingStrategy="RecycleElement" and it worked.