Xamarin形式:刷新列表视图时,当前列表视图开关状态是否更改? [英] Xamarin forms: Current listview switch state changing when the listview gets refresh?

查看:110
本文介绍了Xamarin形式:刷新列表视图时,当前列表视图开关状态是否更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,最初显示20个用户.每当listview底部到达一个新的REST API时,就会开始调用,并显示更多用户(20、40、60等).当加载更多用户时,列表会刷新.

I have a listview, which shows 20 users initially. Whenever the listview bottom reaches a new REST API call will start and it shows more users(20,40,60 etc). When loading more users the list gets refreshed.

Listview项具有切换选项,如果按切换选项,则该用户的用户名将添加到列表中.如果我再次按下相同的开关,则该用户名将从列表中删除.

Listview items have a switch option and if I press the switch option the userid of that user is added to a list. If I again press the same switch that userid is removed from the list.

我的问题是,在加载更多用户时,所选用户的开关将处于关闭状态.但是保存在列表中的用户ID没问题,该列表包含所选用户的ID.那么在加载更多用户后如何在已经选择的用户上切换呢?

推荐答案

从REST服务接收用户时,每个开关的状态为false.因此,您需要保存本地列表.然后,您需要检查您的主键,以查看哪些用户具有state = true.

When receiving the users from your REST service, the state of every switch is false. So you need to save your local list. Then you need to check with your primary key to see which users had state = true.

伪代码:

var oldUsersList = new List<User>(UsersList);
UsersList = await RestService.GetUsers();
foreach (var user in oldUsersList)
{
    if (user.State)
    {
        UsersList.FirstOrDefault(u => u.UserId == user.UserId).State = true;
    }
}


但是您在执行延迟加载时出错!

加载新项目的方式完全消除了您希望完全进行延迟加载的原因.您想要进行延迟加载(=当需要项目时加载),因为您不需要一次加载太多数据.但是,您总是会再次加载所有项目.


But you're doing Lazy Loading wrong!

The way you load new items completely destroys the reasons why you would want to do Lazy Loading at all. You want to do Lazy Loading (= Load when items are needed), because you don't need to load so much data at once. But you always load all items again.

您应该做的总是始终只加载20个项目.然后将这20个项目添加到您的本地列表中.

What you should do is always only load 20 items. Then add these 20 items to your local list.

为此,您需要能够使用offset和limit作为参数来调用REST API.

To do this, you need to be able to call your REST API with offset and limit as parameters.

例如: RestService.GetUsers(长偏移,长限制)

For example: RestService.GetUsers(long offset, long limit)

  1. 加载:RestService.GetUsers(0, 20);
  2. 加载:RestService.GetUsers(20, 20);
  3. 加载:RestService.GetUsers(40, 20);
  4. 加载:RestService.GetUsers(60, 20);
  1. Load: RestService.GetUsers(0, 20);
  2. Load: RestService.GetUsers(20, 20);
  3. Load: RestService.GetUsers(40, 20);
  4. Load: RestService.GetUsers(60, 20);

然后,您可以将结果添加到本地列表中.这样,您无需加载过去已经加载的用户.通过这样做,您也不会出现状态问题,因为您已加载的用户不会更改.

Then you can add the result to your local list. Like this, you don't need to load users you already loaded in the past. Also by doing this, you don't have the problem with the state because your already loaded users don't change.

这篇关于Xamarin形式:刷新列表视图时,当前列表视图开关状态是否更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆