Search code examples
androidgridviewonscrolllistener

OnScrollListener updating GridView


I have a grid view and want to update the items in grid view after the end of list is reached. To implement this i decided to use OnScrollListener. But the behavior of OnScrollListener is strange as it calls my method to update grid view many times..

@Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

                if (firstVisibleItem + visibleItemCount >= totalItemCount) {
                    // End has been reached
                    // Call method to update

                Log.i("CountShit", String.valueOf(firstVisibleItem) + " + "
                        + String.valueOf(visibleItemCount) + " >= "
                        + String.valueOf(totalItemCount));
                }
            }
        });

and in my log the if conditioin is called many times as the onScrollListener keeps on listening the scroll i guess ..

09-29 14:06:10.338: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.353: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.369: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.385: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.400: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.424: I/CountShit(23640): 14 + 6 >= 20
09-29 14:06:10.439: I/CountShit(23640): 14 + 6 >= 20
09-29 14:06:10.455: I/CountShit(23640): 14 + 6 >= 20
09-29 14:06:10.470: I/CountShit(23640): 14 + 6 >= 20
09-29 14:06:10.486: I/CountShit(23640): 14 + 6 >= 20

Is there a way such that if i hit the end of list first time, my upate method is called.. and then if the user scrolls up or down or whatever the method should not be called again! (until i reach the end of my updated list ofcourse).


Solution

  • I was able to solve the problem by adding some variables

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

    and checking the following condition ..

            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                        pageCount++;
                    }
                }
                if (!loading
                        && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                    // End has been reached
                    Log.i("CountShit", String.valueOf(firstVisibleItem) + " + "
                            + String.valueOf(visibleItemCount) + " >= "
                            + String.valueOf(totalItemCount));
                    // call update method here
                    loading = true;
                }
            }
        });