简体   繁体   English

Xamarin Forms SearchBar + ListView更新缓慢

[英]Xamarin Forms SearchBar + ListView slow to update

I would like some help into speeding up the process of filtering a long list of list items and viewing them on a ListView. 我需要一些帮助来加快筛选一长串列表项并在ListView上查看它们的过程。

My app has a search bar, a ListView and a very long list of strings to choose from. 我的应用程序有一个搜索栏,一个ListView和很长的字符串列表供您选择。

When the user enter a search term, the ListView is updated with every key stroke and filter out the irrelevant items. 当用户输入搜索词时,每个按键都会更新ListView并过滤掉不相关的项。

Sorting itself takes a few milliseconds, but updating the ListView afterwards with the new filter-event takes a long time (20 seconds easy, if only a single character has been entered as search criteria) 排序本身需要几毫秒,但是之后使用新的过滤器事件更新ListView则需要很长时间(如果仅输入一个字符作为搜索条件,则需要20秒的时间)

I believe the time is spent on inflating a large number of ViewCells every time the filtered list updates. 我相信每次过滤列表更新时,都会花费大量时间充实大量ViewCell。

Do any of you know how to speed up the process? 你们当中有人知道如何加快流程吗? I thought the way it could work was to have a very limited number of ViewCells (like 10 or 20) and then have them update and just show a selection of the filtered list. 我认为可能的工作方式是使ViewCell数量非常有限(例如10或20),然后更新它们,并仅显示选择的过滤列表。 Scrolling would to be reusing the top/bottom one, update the content and put it back on the bottom/top - but I have not been able to wrap my head around how to do this. 滚动将重用顶部/底部的内容,更新内容,然后将其放回底部/顶部-但我仍无法全神贯注于如何执行此操作。

Maybe it is the wrong approach and you know a better way? 也许这是错误的方法,您知道更好的方法吗?

I just had a similar problem that my list with just 20 elements would search extremely slow. 我有一个类似的问题,我的列表只有20个元素,搜索速度非常慢。 Maybe your problem is similar. 也许您的问题是相似的。 Sadly you didn't post any code. 遗憾的是您没有发布任何代码。 I had something like this: 我有这样的事情:

List l = originalItems.Where((i) => i.Name.Contains(filterText));
listView.ItemsSource = l;

And I could not understand why this would be so slow. 而且我不明白为什么会这么慢。 I found a different approach with more overhead that for some reason is faster and more responsive and overall feels better for the user. 我发现了另一种开销更大的方法,由于某种原因,它更快,响应速度更快,并且总体上让用户感觉更好。 My ListView always has an ObservableCollection as ItemsSource . 我的ListView始终具有一个ObservableCollection作为ItemsSource When I filter I calculate the difference to this ObservableCollection (the extra items and the removed items) and then remove or add accordingly. 当我过滤时,我计算出此ObservableCollection (多余的项目和已删除的项目)的差,然后相应地删除或添加。 This avoids replacing the ItemsSource property which seems to be too harsh on the ListView . 这样可以避免替换ItemsSource属性,该属性在ListView上似乎过于苛刻。

//property of the class
ObservableCollection<FlightListItem> listViewItems;
// ....
//somewhere at initialization
listView.ItemsSoure = listViewItems;
// ....
//in the filter method:
List l = originalItems.Where((i) => i.Name.Contains(filterText));
IEnumerable itemsToAdd = l.Except(listViewItems).ToList();
IEnumerable itemsToRemove = listViewItems.Except(l).ToList();
listView.BeginRefresh();
foreach (FlightListItem item in removes)
    listViewItems.Remove(item);
foreach (FlightListItem item in added)
    listViewItems.Add(item);
listView.EndRefresh();

Notes: 笔记:

  • removing the listView.BeginRefresh() and EndRefresh() did not seem to impact performance, but it seems the right thing to call them here. 删除listView.BeginRefresh()EndRefresh()似乎并没有影响性能,但是在此处调用它们似乎是正确的选择。
  • We need to call ToList() on the itemsToAdd and itemsToRemove even though we only need IEnumerables ! 即使我们只需要IEnumerables我们也需要在itemsToAdditemsToRemove上调用ToList() This is because Except is a lazy operation and will otherwise only be evaluated during the for loop. 这是因为Except是一个惰性操作,否则将仅在for循环期间进行求值。 However during the for loop one of the parameters to Except changes which leads to an IllegalArgumentException due to modifying an IEnumerable while going over it. 但是,在for循环期间,Except的参数之一会更改,这会导致IllegalArgumentException ,这是因为在检查时会修改IEnumerable
  • If anyone knows a good filterable observable collection that would probably be a nicer solution. 如果有人知道一个好的可过滤的可观察集合,那可能是一个更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM