Another solution for appending pages challenge


#1

The task requires us to append the new result with the old one, so here is my code:

@Override protected void onPostExecute(ArrayList<GalleryItem> galleryItems) { if(mItems == null) mItems = galleryItems; else { for (GalleryItem g : galleryItems) { mItems.add(g); } } setupAdapter(); fetchedPage++; }

The only downside so far is that all the time we append the new items, the scroll takes us automatically to the top.


#2

Actually some lil research on StackOverflow helped. So here is my code:

I delcared the variable int gridScrollPosition.

[code]@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0 && currentPage == fetchedPage) {
gridScrollPosition = mGridView.getLastVisiblePosition();
new FetchItemsTask().execute(String.valueOf(currentPage));

                   currentPage++;
               }
        }

@Override
protected void onPostExecute(ArrayList galleryItems) {
if(mItems == null)
mItems = galleryItems;
else {
for (GalleryItem g : galleryItems) {
mItems.add(g);
}
}

        setupAdapter();
        mGridView.setSelection(gridScrollPosition);
        fetchedPage++;

    }[/code]

#3

If you implement this code and you run it on a device with a high resolution screen which can show the whole page on its screen (like nexus 2013), the next page(s) is loaded immediately until page 100.
To prevent this I made the column width 240p instead of 120p.

Maybe one could load 2 pages at once for big screens (or more depending on screen size)? Or does someone else have a more elegant solution.