[英]Xamarin Forms: How to handle big data in ListView?
I need to show very big data in a ListView.我需要在 ListView 中显示非常大的数据。 Must I load all the data once?
我必须一次加载所有数据吗? My code is below:
我的代码如下:
using System.Collections.ObjectModel;
namespace Brunie.Mobile.ViewModels {
public class VmListCompany {
public ObservableCollection<Company> AllCompanies {
get;
} = new ObservableCollection<Company>();
public VmListCompany() {
LoadAllCompanies();
}
private void LoadAllCompanies() {
Company company = null;
while(null != (company = GetNextCompany(company))) {
AllCompanies.Add(company);
}
}
private bool HasNextCompany(Company company) {
bool hasNextCompany = false;
......
return (hasNextCompany);
}
private Company GetNextCompany(Company company) {
if(!HasNextCompany(company)) {
return (null);
}
Company nextCompany = new Company();
......
return (nextCompany);
}
}
public class Company {
public string Name {
get;
set;
}
public string Address {
get;
set;
}
public int NumberOfEmployees {
get;
set;
}
}
}
ListView in Xaml:
Xaml 中的列表视图:
<ListView ItemsSource="{Binding AllCompanies}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Name}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
After called LoadAllCompanies the count of AllCompanies is 20562104. So my app is very slow now.
在调用 LoadAllCompanies 之后,AllCompanies 的数量是 20562104。所以我的应用程序现在非常慢。 How to handle this situation?
如何处理这种情况?
在 API 端使用分页并在 ListItemAppearing 上加载数据
As @Chetan said using API pagination is the best approach .正如@Chetan 所说,使用 API 分页是最好的方法。 That being said if for some reason you can't change the pagination ( let's say its a third party API) you can still improve your performance by optimizing your caching strategy of the listview as shown in the docs
话虽如此,如果由于某种原因您无法更改分页(假设它是第三方 API),您仍然可以通过优化列表视图的缓存策略来提高性能,如文档中所示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.