Question

I have implemented a scroll listener on my ListView with a custom adapter. The idea is, that if the list is scrolling, I do not load data that requires more thinking time (such as checking potentially large tables on the local SQLite DB)

The code I have for the listener is as follows:

    public class ScrollListener : AbsListView.IOnScrollListener
    {
        private readonly ListView _list;

        public ScrollListener(ListView list)
        {
            _list = list;
        }

        #region Implementation of IDisposable

        public void Dispose()
        {
        }

        #endregion

        #region Implementation of IJavaObject

        public IntPtr Handle { get; private set; }

        #endregion

        #region Implementation of IOnScrollListener

        public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
        {
        }

        public void OnScrollStateChanged(AbsListView view, ScrollState scrollState)
        {
            var adapter = (CaseListAdapter) _list.Adapter;
            if (scrollState != ScrollState.Idle)
            {
                adapter.IsScrolling = true;
            }
            else
            {
                adapter.IsScrolling = false;
                adapter.NotifyDataSetChanged();
            }
        }

        #endregion
    }

I am then setting this with:

_caseTable.SetOnScrollListener(new ScrollListener(_caseTable));

This object is definitely being created here, but for some reason the OnScrollStateChanged method is not being called at all, when I start or stop scrolling.

Have I missed something obvious here, or is there a better way to check if a list is scrolling or not? I've been working on getting this ListView scrolling smoothly for a couple of days now and none of the suggestions I've found appear to make any difference or even work correctly.

Was it helpful?

Solution

Wow... Ok, solved this.

I saw a comment on this post:

implementing OnScrollListener for MvxListView

by Cheesebaron... and it works.

Here's my modified code:

            _caseTable.ScrollStateChanged += (o, e) =>
                                                 {
                                                     var adapter = (CaseListAdapter)_caseTable.Adapter;
                                                     if (e.ScrollState != ScrollState.Idle)
                                                     {
                                                         adapter.IsScrolling = true;
                                                     }
                                                     else
                                                     {
                                                         adapter.IsScrolling = false;
                                                         adapter.NotifyDataSetChanged();
                                                     }
                                                 };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top