2

ListView在 Xamarin 页面中。我使用ItemAppearing事件向下滚动。ListViewCell 高度很大,因此首先覆盖 1 个屏幕,然后覆盖第二个视单元的 80%。

加载更多数据以向下滚动的步骤:

  1. 最初在页面加载时,它调用 API 并在 EmployerResult List 中获取 10 条记录。这将使用数据绑定添加到 ListView 中。
  2. ItemAppearing活动。在这个事件中有一个条件。当最后一个单元格开始出现时,它会调用 API 并再次在 ViewModel 的 List 对象中附加 10 条记录。
  3. 因此,每次最后一个单元格开始出现时,它都会调用 API 并附加 10 条记录。

现在重点是每次加载,它会跳过最后一条记录并显示下 10 条记录的第一条记录。但是,如果用户快速向下滚动,有时它会跳过 2-3 条记录。

即,如果我第一次有 10 条记录。现在我在第 9 条记录上,我正在向下滚动到第 10 条。第 10 条记录开始出现,API 调用触发。此调用完成后,屏幕将在屏幕顶部显示第 11 条记录。这里,第 10 条记录被跳过。这样用户将看到第 11 条记录而不是第 10 条。在这里,用户需要再次向上滚动才能看到第 10 条记录。

有时,如果用户快速向下滚动,它会跳过 2-3 条记录。

有人可以建议我吗?

代码

XAML

<ListView Grid.Row="0" x:Name="EmployerResultsListView"
          ItemsSource="{Binding EmployerResults}"
          HasUnevenRows = "true"
          SeparatorVisibility="None"
          IsPullToRefreshEnabled="true"
          RefreshCommand="{Binding RefreshCommand}"
          IsRefreshing="{Binding IsRefreshing, Mode=OneWay}"
          ItemAppearing="Handle_ItemAppearing"
          ItemTapped="OnEmployerResultsListViewItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <local:EmployerResultViewCell />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

XAML.CS

private void Handle_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
    var itemTypeObject = e.Item as EmployerProfile;
    if (_viewModel.EmployerResults.Last() == itemTypeObject && _viewModel.EmployerResults.Count() != 1)
    {
        if (_viewModel.LoadMoreCommand.CanExecute(null))
        {
            _viewModel.LoadMoreCommand.Execute(null);
        }
    }
}

视图模型

public EmployerResultsViewModel()
{
    LoadMoreCommand = new RelayCommand(LoadMoreEmployerResult, () => !IsBusy);
    EmployerResults = new ObservableRangeCollection<EmployerProfile>();
}
public ObservableRangeCollection<EmployerProfile> EmployerResults { get; set; }

private async void LoadMoreEmployerResult()
{
    IsBusy = true;
    EmployerResults.AddRange((await _employerApiClient.GetMoreData(pagenumber)));
    IsBusy = false;
}
4

2 回答 2

1

据我了解,您正在尝试进行延迟加载。

-首先,您应该像这样设置回收策略: CachingStrategy="RecycleElement" 如果您想要可接受的性能,如此处所述https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview /performance,然后重新测试 ItemAppearing 事件的行为。

- 然后,使用或分析满足您需求的现有组件可能是一个好主意。例如:http: //15mgm15.ghost.io/2017/11/28/implement-an-infinite-scrolling-listview-with-xamarin-forms/

于 2018-05-22T15:01:26.950 回答
1

我所做的是在末尾添加额外的空白单元格,LoadMoreEmployerResult并要求加载更多内容。在加载更多时,我删除了那个空白单元格。这是我认为可以解决我的问题的唯一方法。

private async void LoadMoreEmployerResult()
{
    IsBusy = true;
    if(EmployerResults.Last().Name == "")
        EmployerResults.RemoveAt(EmployerResults.Count - 1);
    List<EmployerProfile> currentPageList= await _employerApiClient.GetMoreData(pagenumber);
    if(currentPageList.Count > 0)
    {
        EmployerResults.AddRange(currentPageList);
        EmployerResults.Add(new EmployerProfile());
    }

    IsBusy = false;
}
于 2018-06-18T04:43:26.437 回答