【问题标题】:Xamarin Forms: How to handle big data in ListView?Xamarin Forms:如何在 ListView 中处理大数据?
【发布时间】:2020-02-13 10:09:43
【问题描述】:

我需要在 ListView 中显示非常大的数据。我必须加载所有 数据一次?我的代码如下:

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;
        }
    }
}

Xaml 中的列表视图:

<ListView ItemsSource="{Binding AllCompanies}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding Name}" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

调用 LoadAllCompanies 后,AllCompanies 的计数为 20562104。 所以我的应用程序现在很慢。 这种情况如何处理?

【问题讨论】:

标签: forms listview xamarin


【解决方案1】:

在 API 端使用分页并在 ListItemAppearing 上加载数据

【讨论】:

    【解决方案2】:

    正如@Chetan 所说,使用 API 分页是最好的方法。 话虽如此,如果由于某种原因您无法更改分页(假设它是第三方 API),您仍然可以通过优化列表视图的缓存策略来提高性能 如图docs

    【讨论】:

    • 嗨@King23 但在普通用户中不检查 20562104 记录,所以为什么我们需要在 ListView 中加载数据。我认为我们需要在 List 中加载数据并在 UI 端使用分页
    • @ChetanRawat 不要误解我的意思。你的答案是对的,分页是要走的路,但鉴于他有很多记录,如果用户不断滚动浏览分页页面,建议优化列表视图以进一步增强用户体验
    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多