简体   繁体   中英

Xamarin Forms Collection View Pagination

I want to achieve pagination in xamarin forms, actually I have not done this for any mobile application so this is my first time. I am using collection view and remainingItemThreshold and the event is being triggered but the items are not being added. I think this is a good logic to go through but I think I am missing something. Thanks in advance!

        private async void Init()
    {
        SetViews();

        Methods.SetFlowDirection(this);

        Methods.BeforeChecking(activityIndicator, parent);

        GetOrdersApiResponse response = await OrdersPageLogic.GetOrders();

        Methods.AfterChecking(activityIndicator, parent);
        //
        // get all orders
        orders = response.Orders;
        
        toRange = orders.Count >= pagination ? pagination : orders.Count;

        // only show first 10 items
        ordersToShow = orders.GetRange(0, toRange);

        // remove fetched items
        orders.RemoveRange(0, toRange);

        ordersCollView.ItemsSource = ordersToShow;
        
        ordersCollView.RemainingItemsThreshold = 2;

        ordersCollView.RemainingItemsThresholdReached += (s, e) => ordersCollView_RemainingItemsThresholdReached(s, e);
    }

    private void ordersCollView_RemainingItemsThresholdReached(object s, EventArgs e)
    {
        int count = 0;
        int to = 0;

        if (orders.Count == 0)
            return;

        to = pagination >= orders.Count ? orders.Count : pagination;

        foreach(var order in orders.GetRange(0, to))
        {
            if (count == pagination)
                break;

            ordersToShow.Add(order);
            count++;
        }

        // remove fetched items
        orders.RemoveRange(0, to);
    }

So thanks to @Jason it appeared that the item source (in my case ordersToShow) have to be observable collection not list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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