6

I'm having problem with my setOnScrollListener. It just keeps calling my asynctask whenever I scroll to the bottom of the listview. How do I set the setOnScrollListener to load only once I reach the bottom.

listview.setAdapter(adapter);
mProgressDialog.dismiss();

listview.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
        // TODO Auto-generated method stub
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        int lastInScreen = firstVisibleItem + visibleItemCount;
        if (lastInScreen == totalItemCount) {
            new loadmore().execute();
        } else {
        }
    }
);
MontrealDevOne
  • 1,034
  • 3
  • 17
  • 30
KC Chai
  • 1,607
  • 9
  • 30
  • 59

1 Answers1

13

OnScroll method is called whenever you scroll down the list view, so the best bet for you to is to use some kind of padding, like the one implemented here.

public class EndlessScrollListener implements OnScrollListener 

    private int visibleThreshold = 5;
    private int currentPage = 0;
    private int previousTotal = 0;
    private boolean loading = true;

    public EndlessScrollListener() {
    }
    public EndlessScrollListener(int visibleThreshold) {
        this.visibleThreshold = visibleThreshold;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            // I load the next page of gigs using a background task,
            // but you can call any function here.
            new LoadGigsTask().execute(currentPage + 1);
            loading = true;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
}

visibleThreshold – The minimum amount of items to have below your current scroll position, before loading more.

currentPage – The current page of data you have loaded

previousTotal – The total number of items in the dataset after the last load

loading – True if we are still waiting for the last set of data to load.

Mushtaq Jameel
  • 7,053
  • 7
  • 33
  • 52
  • Anytime bro, just helping a brother out :-P. – Mushtaq Jameel Jul 04 '13 at 16:01
  • @mushtaq can you please tell me what should we code in `class LoadGigsTask()` i mean the logic behind it. and how to use the `currentPage` in webservice(like json) to retrieve more posts and update listview. – Ankit Dhadse Nov 12 '13 at 09:04
  • @Ankxx13 - Sorry I could not reply immediately. But that is basically an asynchronous call in android. This is basically done to do any push any heavy lifting acting to a background thread so that the main screen does not freeze.Here you can do a Web Service call to and get a JSON response and parse it and add it to the list view, the currentPage acts like a marker telling you @ which record you should fetch from, you all this logic would go into your back-end that does the data processing. – Mushtaq Jameel Dec 12 '13 at 06:00
  • 1
    @Ankxx13 - Here are some links that might be useful http://java.avdiel.com/Tutorials/JDBCPaging.html http://theopentutorials.com/examples/java-ee/jsp/pagination-in-servlet-and-jsp/ – Mushtaq Jameel Dec 12 '13 at 06:03
  • @mushtaq Thank you for your response. Now i understood what i should code(Query) in the back-end file. The attribute LIMIT is all i should balance of queering – Ankit Dhadse Dec 12 '13 at 06:12
  • it calls the paging method multiple times on fling – Ravi Apr 21 '14 at 09:02
  • @mushtaq can you help ?? http://stackoverflow.com/questions/28122304/endless-scrolling-listview-not-working –  Jan 28 '15 at 06:57