Refining the paging challenge further


In my implementation of the photo gallery I have noticed that every time I download another page and append it to my previous page of "images"
I have to scroll through all the pages just to get to where I was before
I found a number of suitable methods in AbsListView documentation which would be helpful to move to the required record
First I tried setVerticalScrollbarPosition(int position) but I got a warning that I needed API 11 for it to run
So because I am lazy I chose smoothScrollToPosition(int position) which works, but In a way that the user would appreciate less

So my extra credit challenge for all of you is to make the appended paging pick up from where it has left off


gridView.setOnScrollListener(new onScrollListener(){ @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) if(firstVisibleItem + visibleItemCount >= totalItemCount){ nextPage++; // nextPage is an int that keeps track of the pages loaded. ArrayList<GalleryItem> newItems = new FlickrFetchr().fetchitems(nextPage.toString()); // Update fetch items to accept a string for the page number and use that to supply the parameter to the GET request items.addAll(newItems); // items is your current ArrayList<GalleryItem> ArrayAdapter adapter = (ArrayAdapter)mGridView.adapter; adapter.addAll(newItems); adapter.notifyDataSetChanged(); } } });

To be honest, I have been coding all of the examples in C# using Xamarin, so I had to convert back to Java so I did not check this. Another thing I found I had to do was to use a boolean to make sure that I only executed the code in the if statement once since I noticed that the if conditional returned true more than once when you reached the end. Here is my C#/.NET version that works:

mGridView.Scroll += async (object sender, AbsListView.ScrollEventArgs e) => {
    if (e.FirstVisibleItem + e.VisibleItemCount == e.TotalItemCount && !endReached && e.TotalItemCount > 0) {
         endReached = true;
         List<GalleryItem> newItems = await new FlickrFetchr().Fetchitems(currentPage.ToString());
         galleryItems = galleryItems.Concat(newItems).ToList();
         var adapter = (ArrayAdapter)mGridView.Adapter;
         endReached = false;

But basically using notifyDataSetChanged() after adding the new items to the ArrayAdapter was the way I found to avoid the auto-scrolling. I was using smoothScrollToPosition() as well initially.