-4

I need to create a listView that loads 10 downloaded items at a time (they are stored on a Json object) and then when the user scrolls to the last element the listView loads another 10 items and so on.. I tried various solutions with external projects I found but none of them worked, and now I'm wondering, Is it possible to download only n items at a time using Json and then implement a listView listener so when the user scrolls to the last element of the list the background task that downloads the Json gets called again?

LS_
  • 6,763
  • 9
  • 52
  • 88

2 Answers2

1

I'm using a scroll listener like this one:

public class AutomaticLoadScrollListener implements AbsListView.OnScrollListener {

    private int currentFirstVisibleItem;
    private int currentVisibleItemCount;

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.currentFirstVisibleItem = firstVisibleItem;
        this.currentVisibleItemCount = visibleItemCount;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (currentVisibleItemCount > 0 && currentScrollState == SCROLL_STATE_IDLE) {
            /*** In this way I detect if there's been a scroll which has completed ***/
            /*** do the work for load more date! ***/

            // LOAD DATA
        }
    }
}

Just set it as scrollListener and it should work:

listView.setOnScrollListener(new AutomaticLoadScrollListener());

Credits: I found it here

Community
  • 1
  • 1
Vincent Durmont
  • 813
  • 8
  • 16
1

Think about building pagination system, get records page by page, when scroll finish to end get next 10 item, then get next 10 item until reach to your list size.

erhanasikoglu
  • 1,685
  • 1
  • 21
  • 33
  • yeah I just found an example that does that but I don't know how to create the PHP that auto generates the XML based of the page passed – LS_ Jan 13 '15 at 08:37