Ripal Tamboli
Ripal Tamboli

Reputation: 572

ListView OnScroll event executing multiple times for same position

I am using listview to display comments. For that, i am implementing lazy listview loading as it may be possible that there are 50 comments in database. so i am rendering comments partially.

For testing i am right now making request for 3 comments. When i reached to 3rd row, i am again requesting for next 3 comments. Now problem is when i scrolled to 3rd row, Onscroll executing multiple times for 3rd row resulting multiple request to server instead of one time next 3 comments request.

Below is the code:

@Override
    public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {

        Log("isFirtTime - "+firstTimeLoaded+" ** noMore -"+ noMoreCommentsFound);
        if(firstTimeLoaded==false && noMoreCommentsFound==false) {
            Log("firstVisibleItem - "+firstVisibleItem+" ** visibleItemCount -"+ visibleItemCount+" ** totalItemCount"+totalItemCount);
            if(firstVisibleItem+visibleItemCount==totalItemCount) {
                if(FviewsAPI.isNetworkAvailable(getApplicationContext())) {
                    commentStartIndex+=3;                   
                    FviewsAPI.getMovieComments(movieId, commentStartIndex, commentEndIndex, MovieDetails.this);
                }
            }
        } 
    }

Below is the LogCat Details to track the scrolling events:

11-19 12:21:21.842: V/FVIEWS(10358): isFirtTime - false ** noMore -false
11-19 12:21:21.842: V/FVIEWS(10358): firstVisibleItem - 0 ** visibleItemCount - 2 ** totalItemCount - 3
11-19 12:21:21.862: V/FVIEWS(10358): pos : 2

11-19 12:21:21.872: V/FVIEWS(10358): isFirtTime - false ** noMore -false
11-19 12:21:21.872: V/FVIEWS(10358): firstVisibleItem - 0 ** visibleItemCount - 3 ** totalItemCount - 3
11-19 12:21:21.872: V/URL(10358): ~~URL - http://***.***.*.100/dipen/****_new/web/viewAllComments/14/3/3

11-19 12:21:21.882: V/FVIEWS(10358): isFirtTime - false ** noMore -false
11-19 12:21:21.882: V/FVIEWS(10358): firstVisibleItem - 0 ** visibleItemCount - 3 ** totalItemCount - 3
11-19 12:21:21.882: V/URL(10358): ~~URL - http://***.***.*.100/dipen/****_new/web/viewAllComments/14/6/3

11-19 12:21:21.892: V/FVIEWS(10358): isFirtTime - false ** noMore -false
11-19 12:21:21.892: V/FVIEWS(10358): firstVisibleItem - 0 ** visibleItemCount - 3 ** totalItemCount - 3
11-19 12:21:21.892: V/URL(10358): ~~URL - http://***.***.*.100/dipen/****_new/web/viewAllComments/14/9/3

If you see the logcat details, when 3rd row appear on screen, onScroll event logging the "visibleItemCount - 3" multiple times ( WIDTH OF THE LISTVIEW SET TO ONLY DISPLAY TWO ROWS ON SCREEN ) hence, it is calling multiple times the comment api intsead of only one time next 3 comments api.

WHAT ANDROID DOC SAYS IS "onscroll event will be called after the scroll has completed" here is the link of doc : http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html#onScroll(android.widget.AbsListView, int, int, int)

Then Why multiple times it is calling for 3rd row?? any Idea??

Upvotes: 2

Views: 3188

Answers (1)

Gunaseelan
Gunaseelan

Reputation: 15535

onscroll event will be called after the scroll has completed

Which means onscroll event will be called after the last item of listview comes to visible. I hope this help you.

Upvotes: 2

Related Questions