programmatically move items in UWP ListView without refresh

二次信任 提交于 2019-12-11 06:34:25

问题


The following problem is keeping me busy now for quite some time, it seems to be so basic, yet it just doesn't work. It boils down to this:

  • Having a ListView that is bound to some suitable collection in code behind (ObservableCollection, ReactiveList, or alike)
  • I'm moving items one at a time every x seconds
  • on every move, all of the items get refreshed (at least it looks like that. for a split seconds all items disappear, then reappear in the new order)

there must be a way to keep the other element and just "move" the moved item. I even don't care about a fancy translation animation for the moved item, I just want the other elements to stay on screen. of course my real usecase is not moving items randomly but sorting the list in code behind. But I tracked the issue down to this simple case.

having a simple ListView and having it bound e.g. to a ObservableCollection in CodeBehind, the following is my dummy code to move around the items:

_timer = new Timer(async _ =>
{
    Random r = new Random();
    var randomIndex = r.Next(0, contactsCvsSource.Count - 1);

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { contactsCvsSource.Move(randomIndex, 5); });
}
, null, 0, 1000);

回答1:


on every move, all of the items get refreshed (at least it looks like that. for a split seconds all items disappear, then reappear in the new order

The Move method of ObservableCollection is used to improve UI refresh performance. The UI element hash value will not change when Move method invoke, it just refresh the ListView and did not recreate.

I just want the other elements to stay on screen. of course my real usecase is not moving items randomly but sorting the list in code behind.

For your requirement, you could realize this feature via Insert and Remove method.

var _timer = new Timer(async _ =>
 {
     Random r = new Random();
     var randomIndex = r.Next(0, Items.Count - 1);
     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
         Items.Insert(5,Items[randomIndex] );
         Items.RemoveAt(randomIndex);                   
     });
 }
 , null, 0, 1000);      


来源:https://stackoverflow.com/questions/50132820/programmatically-move-items-in-uwp-listview-without-refresh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!