-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Bug] [Android] CollectionView jittering on Android with GridItemsLayout #8718
Comments
I don't see anything weird with how the images are loaded. Images are not too big. Yet the scrolling is not very smooth. |
Yes. But on ios scrolling is very smooth. |
Same problem here... |
@Alex-111 Tried latest stable version and still the same. But a preview of 4.6.0 looks a bit better |
Also just tried the preview of 4.6. Unfortunately not much change here…. |
Hello. Same problem on my side... Even with LinearItemsLayout After analysis with Xamarin Profiler, it seems there's a lot of BindableProperty and linked mechanisms created in memory when scrolling (BindablePropertyContext, ...). I guess, that's a consequence of recycling... When i don't use DataBinding, things are better but not perfects... Not relying on DataBinding specially with images is advised by the FFIL author for example.... I've also seen the same kind of problems with ListView... If someone has some best practices/solutions on this topic, it will be really appreciated :) |
I can confirm this. Lots of garbage collections in the debug output… But not sure if this alone is the reason for the heavy jitter.. |
Any progress on this issue? This is causing serious issues within our project and we can't release due to this issue |
Latest version in combination with GlideX.Forms looks pretty good but still not perfect as iOS version of CollectionView |
@JMNewsome Don't expect the Xamarin.Forms team to be quick on resolving issues... |
Any news on this? This one makes developing on Android really painful.... |
I'm seeing the same behavior in my GitTrends app. Here's a video one of the users posted on Twitter running GitTrends v1.2.0:
GitTrends v1.2.0 Source Code: |
Yeah, this is a very big performance problem, can we get some kind of higher priority on this item? |
WorkaroundI created a blog post regarding this: I found that two things were causing the UI to jitter when the user scrolls a CollectionView:
I was able to remove the jitter by removing bindings from my If you'd like to see exactly how I solved it, here is the Pull Request that fixed it in my GitTrends app: TheCodeTraveler/GitTrends#143
Removing DataTemplate BindingsTo remove bindings, we can use a Before Removing DataTemplate Bindingsusing Xamarin.Forms.Markup;
class CollectionViewPage : ContentPage
{
Content = new CollectionView
{
ItemTemplate = new ImageDataTemplate()
}.Bind(CollectionView.ItemsSourceProperty, nameof(CollectionViewModel.ImageList));
}
class ImageModel
{
public string ImageTitle { get; set; }
public string ImageUrl { get; set; }
}
class ImageDataTemplate : DataTemplate
{
public MyDataTemplate() : base(() => CreateDataTemplate())
{
}
static View CreateDataTemplate() => new StackLayout
{
Children =
{
new Image().Bind(Image.SourceProperty, nameof(ImageModel.ImageUrl))
new Label().Bind(Label.TextProperty, nameof(ImageModel.ImageTitle))
}
}
} After Removing DataTemplate Bindingsusing Xamarin.Forms.Markup;
class CollectionViewPage : ContentPage
{
Content = new CollectionView
{
ItemTemplate = new ImageDataTemplateSelector()
}.Bind(CollectionView.ItemsSourceProperty, nameof(CollectionViewModel.ImageList));
}
class ImageModel
{
public string ImageTitle { get; set; }
public string ImageUrl { get; set; }
}
class ImageDataTemplateSelector : DataTemplateSelector
{
protected override DataTemplate OnSelectTemplate(object item, BindableObject container) => new ImageDataTemplate((ImageModel)item);
class ImageDataTemplate : DataTemplate
{
public MyDataTemplate(ImageModel imageModel) : base(() => CreateDataTemplate(imageModel))
{
}
static View CreateDataTemplate(ImageModel imageModel) => new StackLayout
{
Children =
{
new Image { Source = imageModel.ImageUrl },
new Label { Text = imageModel.ImageTitle }
}
}
}
} Increate Nursery Size to Decrease Garbage CollectionWe can set the size of the Android Nursery by setting it in the
MONO_GC_PARAMS=nursery-size=64m Note:
|
Thank you @brminnick . I will try this. |
@brminnick : Thanks for suggesting some workarounds!
any further suggestions are appreciated... |
I think this garbage collection issue is the number one factor in Xamarin Forms for Android running terribly and should be something that is prioritized. |
It's pretty sad to see Xamarin go down this path. The listview is not recommended any more and collection view is pretty much not usable. As a developer I dont know how am I gonna use xamarin to build apps, or even support the apps that we have build over the years. |
I agree, what I love about Xamarin is the ability to use C# and shared between the platform, code execution speed for me on a compiled app is NOT the problem. The problem is the UI thread, which of course will cause everything else to appear to run bad. If the UI thread didn't feel like it was held together with duct tape and staples, It would be so much better |
@brminnick, is this an issue when creating UI controls from code behind? I tested this out with one of my CollectionView templates. Creating UI control from code behind causes the Nursery to become full frequently, in turn calling the GC constantly. Using XAML, Nursery full warning is less frequent. |
@Deepfreezed Can you share your reproduction code and its associated performance testing results? As far as I'm aware, this is not a XAML-specific nor a C#-specific problem. Rather, it is a problem related to bindings and garbage collection. |
Same issue is here for me. Android performance is very bad. |
Will the issue be resolved in the next release of Xamarin Forms? Due to this issue, we are unable to distribute the application. |
This problem is really very serious, I changed the listview for collectionview by official recommendation, now a problem like this arises. It is very sad to see that it has been over a year and the problem has not been solved, the solution @brminnick does not sound very good to me due to all the efforts made, but it solved this problem in a demo I made, we have an application with 5 million downloads made in xamarin and we often have a crash and only now I discovered that this was the real problem, what a shame. |
I had this problem on a production app. I need this fix in the 5.0 milestone. |
I replaced the CollectionView with a StackLayout and Datatemplate. This works for me. |
@AlleSchonWeg Replacing a CollectionView with a StackLayout and Datatemplate is not a proper solution as it does not support virtualization, so you will see an impact the more items you have |
Hello. We achieved step by step to deliver a good experience to our customers in terms of performances with our CollectionView containing lot of content (Images, Buttons, Texts, ...). I have one advice and one trick that could perhaps help some of you...
|
I also noticed that the GetItemViewType was not correctly implemented on Android. public override int GetItemViewType(int position)
{
if (_isDisposed)
{
return -1;
}
if (_element.ItemTemplate is DataTemplateSelector dataTemplateSelector)
{
var dataTemplate = dataTemplateSelector.SelectTemplate(_dataSource[position], _element);
int itemViewType = _dataTemplates.IndexOf(dataTemplate);
if (itemViewType == -1)
{
itemViewType = _dataTemplates.Count;
_dataTemplates.Add(dataTemplate);
}
return itemViewType;
}
return base.GetItemViewType(position);
} |
The same issue here... |
Thank you Brandon for saving my life!! I found that correcting the GC solved my collection view scroll jittery problem. Android default is 512KB, which is very small. I changed it to 32MB and its silky smooth now. Side Note:
|
Today I spent a few hours trying to further improve the scrolling, which Mr Jitter came back again! I'd like to share what I did, what worked and what did not work to improve the jittery scrolling WHAT DID NOT WORK
WHAT WORKED
VERDICT => BINDABLE LAYOUT PERFORMANCE IS BAD.... DONT USE IT . On further reasearch I realized that this has been mentioned on Microsoft Website:- Bindable layouts should only be used when the collection of items to be displayed is small, and scrolling and selection isn't required. While scrolling can be provided by wrapping a bindable layout in a ScrollView, this is not recommended as bindable layouts lack UI virtualization. When scrolling is required, a scrollable view that includes UI virtualization, such as ListView or CollectionView, should be used. Failure to observe this recommendation can lead to performance issues. Source => https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/bindable-layouts |
This is still an issue, and none of the workarounds seem to work. In my scenario, each item is a single label with a single one-time binding on The first 500 or so items scroll fine, but after that it gets slower and slower until around the 1000th item it's literally unusable. Somewhere around 1500 items the app crashes. 1000 items should be a fairly common scenario, so it's absurd that there's seemingly no way to get this to work. |
You could try Sharpnado ListView: https://github.com/roubachof/Sharpnado.HorizontalListView. It supports grid, horizontal and vertical layout. |
@AlleSchonWeg Yes I'm aware of that one, along with several other open-source options but none of them support groupings. |
It's really hard to have an Android app that is fluid with Xamarin. |
I tested the CollectionView on MAUI in Android, it's working great, you can migrate to Maui, but it is still in the preview Version . |
@brminnick Hi. Could it create serious problems or memory leak? |
This seems interesting: Sees significant performance improvments when swapping XF bindings with his own implementation. 🥰 |
Description
I tried to reproduce UI of one popular music app (DI.FM).
During implementation of page with styles I have encountered issue with collectionview and images. Fast scroll on CollectionView with lots of images produces jittering on Android.
I have tried Image, ffImageLoading:CachedImage (a bit better), Image with GlideX(a bit better).
While it works perfectly smooth on ios even with ordinary Image control. Also I have tried different sizing strategies.
Steps to Reproduce
Expected Behavior
No jittering, smooth scrolling
Actual Behavior
Jittering
Basic Information
Reproduction Link
DIFM.zip
The text was updated successfully, but these errors were encountered: