简体   繁体   中英

Web API call without setOnScrollListener() of Listview pagination in android

Summary of work Environment

I'm working on android application where we have to display the product list. There is a Web API for that with pagination. Normally we use the setOnScrollListener() function and when user scroll the list then web api called for next page and it load the data.

Requirement

Get the next page data from web api without using setOnScrollListener() function in Listview.

Let me explain in detail : Firstly Product api called for page number 1, get data from server and set in listview, now I want to load the next page (2) data from server and set in listview and so on without using setOnScrollListener() function.

Load the product list like Flipkart and other eCommerce apps with taking

Working code with setOnScrollListener()

Here is the code of using setOnScrollListener() function. As user scroll down and come to last item then it hits the web api and load the data for next page and so on..

mProductGV.setOnScrollListener(new EndlessScrollListener() {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            // Triggered only when new data needs to be appended to the list
            // TotalPage is number of pages which is come from server.. 
            if (page > TotalPage) {

            } else {
                pageNum = page;
                // hit api to get the product data for next page.
                getProductData();
            }
        }
        @Override
        public void onLoadViewItem(int viewItem, int TotalItem){

        }
    });

What I tried

By using the TimerTask , I called the api to get data from server and load it..

private final static int DELAY = 4000;
private final Handler handler = new Handler();
private final Timer timer = new Timer();
private final TimerTask task = new TimerTask() {
    private int counter = 1;
    public void run() {
        handler.post(new Runnable() {
            public void run() { 
                getproductList(0);
                pageNum++;
            }
        });
        if(++counter > TotalPage) {
            timer.cancel();
        }
    }
};


 // call in on create function..
 timer.schedule(task, DELAY, DELAY);

Problem

  1. timer task stop its execution when go to detail screen and when I come back to listing screen then it does not load the next page data and I've to load product data again from page 1..

  2. Same case for when I go for Filter/sort the product list data then stop the prev task and restart timer task again from page 1.

I'm confused in this.. I know there is some silly mistake that I'm not able to catch. So please suggest any example of like that or any other best good solution for this..

Thanks..

You can override getItem() in your BaseAdapter to have callbacks, when new items are added to ListView. By comparing with items list size you can decide if it's good moment to fetch more data.

public Object getItem(int position) {
    super.getItem(position);
    if (closeEnoughToPullData(position)) {
        pullData();
    }
}

// will return true, if loaded item 3 items away from end
public boolean closeEngoughToPullData(int position) {
    return items.size() - position < 3;
}

May not be exact solution to your problem, but getItem() invocations are much less frequent and should help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM