Custom ListView with Generic arguments

MigokMigok Member ✭✭
edited May 28 in Xamarin.Forms

Hello,

In my app, i have a lot of ListView with 5000+ items. Its very very slow to display.
So I want to create a ListView, in wich I add items while user is scrolling.

But as I said, I have multiple ListView like this, using differents objects. So I would like to create a generic List.

My code below.

public class CustomListView : ListView
    {
        private ObservableCollection<T> DisplayedList;
        private List<T> CompleteList;

        public void Init(List<T> mCompleteList, ObservableCollection<T> mDisplayedList)
        {
            DisplayedList = mDisplayedList;
            CompleteList = mCompleteList;

            ItemsSource = DisplayedList;

            ItemAppearing += ListeView_ItemAppearing;
        }

        private void ListeView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
            T curItem = (T) e.Item;
            int nbDisplayed = DisplayedList.Count;
            int idxCurGroup = DisplayedList.IndexOf(curItem);

            if (nbDisplayed - idxCurGroup < 20)
            {
                int NbAdds = 20;

                for (int i = nbDisplayed; i < CompleteList.Count && NbAdds > 0; i++)
                {
                    DisplayedList.Add(CompleteList[i]);
                    NbAdds--;
                }
            }

        }
    }

It says T is "unknown". But when try this
public class CustomListView<T> : ListView
it doesnt compile.

I tried replacing T with "object", it doesnt work because i cant cast the real Object into "object".

Any idea how i could achieve this ?

Best Answer

Answers

  • MigokMigok Member ✭✭

    Thanks for your answer :smile:

    But you do realize there are plenty of ready-made ListView controls with paging already built-in, right?

    Do you have an example ? I did not find what I am looking for. And I am not big fan of paging system, I want use only the scroll to navigate between items.

    Don't cast to object. Just send your widget. It already inherits from object and will pass just fine.

    Weird, when i try to cast a single object it works, but when i want to cast a list of this object, it doesnt :

    //works
    object obj = new Grouping<PDV, PDV>(new PDV(null));
    
    //dont works
    ObservableCollection<Grouping<PDV, PDV>> DisplayedPDVList = new ObservableCollection<Grouping<PDV, PDV>>();
    List<Grouping<PDV, PDV>> PDVList = new List<Grouping<PDV, PDV>>();
    listeViewPDVs.Init(PDVList, DisplayedPDVList);
    

    Here is the error :

    Argument 1: conversion impossible from 'System.Collections.Generic.List<Portwin.Portwin.CustomObjects.Grouping<Portwin.Portwin.Pdv.PDV, Portwin.Portwin.Pdv.PDV>>' to 'System.Collections.Generic.List<object>' Portwin.Android, Portwin.iOS C:\PROJETS\XAMARIN\PORTWIN XAMARIN\Portwin\Portwin\Portwin\Portwin\Pdv\PageListePDVs_v2.xaml.cs

  • MigokMigok Member ✭✭

    Ok neverming, I just changed
    List<Grouping<PDV, PDV>> PDVList = new List<Grouping<PDV, PDV>>();
    into
    List<object> PDVList = new List<object>();
    Now its working great =)

Sign In or Register to comment.