implementing Infinity scrolling to ListView in Xamarin.Android

avskmkavskmk Member ✭✭

Hi
Want to implement infinity scrolling/lazy loading to ListView.
after searching about infinity scroll, found the solution with "SetOnScrollListener". But when I implemented, getting the following error

        {System.NotSupportedException: Unable to activate instance of type LocationTrack.Helper.LazyLoader from native handle 0xbfd3453c (key_handle 0xb3b4f7a). ---> System.MissingMethodException: No constructor found for LocationTrack.Helper.LazyLoader::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
       --- End of inner exception stack trace ---
      at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00055] in <b781ed64f1d743e7881ac038e0fbdf85>:0 
      at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00116] in <b781ed64f1d743e7881ac038e0fbdf85>:0 
       --- End of inner exception stack trace ---
      at (wrapper dynamic-method) System.Object.20(intptr,intptr,intptr,int,int,int)
      at (wrapper native-to-managed) System.Object.20(intptr,intptr,intptr,int,int,int)
      at (wrapper managed-to-native) Java.Interop.NativeMethods.java_interop_jnienv_call_void_method_a(intptr,intptr&,intptr,intptr,Java.Interop.JniArgumentValue*)
      at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00053] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
      at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
      at Android.Widget.AbsListView.SetOnScrollListener (Android.Widget.AbsListView+IOnScrollListener l) [0x00036] in <b781ed64f1d743e7881ac038e0fbdf85>:0 
      at LocationTrack.LocationLogs.LoadLogs () [0x0005a] in D:\Practice\Xamarin\LocationTrack\LocationTrack\LocationLogs.cs:65 }

Here is my implementation to load the data

        lstViewData = FindViewById<ListView>(Resource.Id.lst_logs);
        var adapter = new ListViewAdapter(this, listSource);
                lstViewData.Adapter = adapter;
                lstViewData.SetOnScrollListener(new Helper.LazyLoader());

And my LazyLoader class

        public class LazyLoader : Java.Lang.Object, AbsListView.IOnScrollListener
            {
                public IntPtr Handle
                {
                    get
                    {
                        return new IntPtr();
                    }
                }

                public LazyLoader() : base()
                {

                }


                private bool loading = true;
                private int previousTotal = 0;
                private int threshold = 10;

                public void Dispose()
                {
                    //throw new NotImplementedException();
                }

                public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
                {
                    if (loading)
                    {
                        if (totalItemCount > previousTotal)
                        {
                            // the loading has finished
                            loading = false;
                            previousTotal = totalItemCount;
                        }
                    }

                    // check if the List needs more data
                    if (!loading && ((firstVisibleItem + visibleItemCount) >= (totalItemCount - threshold)))
                    {
                        loading = true;

                        // List needs more data. Go fetch !!
                        loadMore(view, firstVisibleItem,visibleItemCount, totalItemCount);
                    }
                }

                public void OnScrollStateChanged(AbsListView view, [GeneratedEnum] ScrollState scrollState)
                {

                }

                // Called when the user is nearing the end of the ListView
                // and the ListView is ready to add more items.
                public void loadMore(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
                {

                }
            }

What i'm missing here. can anyone help.

Thanks

Best Answer

  • avskmkavskmk ✭✭
    Accepted Answer

    Achieved by Scroll event.
    Instead of using SetOnScrollListener callback used Scroll event to get the next set of records when user scrolls to end.

            bool loading = true;
            private void LstViewData_Scroll(object sender, AbsListView.ScrollEventArgs e)
                    {
                        if (loading)
                        {
                            if (e.TotalItemCount > previousTotal)
                            {
                                // the loading has finished
                                loading = false;
                                previousTotal += 20; //20 = page count
                            }
                        }
    
                        // check if the List needs more data
                        if (!loading && ((e.FirstVisibleItem + e.VisibleItemCount) >= (e.TotalItemCount)))
                        {
                            loading = true;                         
                            // List needs more data. Go fetch !!
                            LoadLogs(previousTotal, e.FirstVisibleItem);
    
                        }
                    }
    

Answers

Sign In or Register to comment.