hacker
hacker

Reputation: 8947

Gridview onScroll method gets called always, without user scroll

I have a customized gridview where i'm checking onScroll method to find the end of the list. If the scroll reaches the end of the list, it will again add few elements in to the list.

gridview.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView arg0, int arg1) {

        }

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

            int lastInScreen = firstVisibleItem + visibleItemCount;     


            //is the bottom item visible & not loading more already ? Load more !
            if((lastInScreen == totalItemCount) && (!loadingMore))
            {   
                new LoadDataTask().execute();

            } 
        }
    });

And this is my Asynchronous task class..

    private class LoadDataTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        if (isCancelled()) {
            return null;
        }
        loadingMore = true;
        for (int i = 0; i < mNames.length; i++)
            mListItems.add(mNames[i]);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mListItems.add("Added after load more");


         loadingMore=false;
        adapter.notifyDataSetChanged();
        super.onPostExecute(result);
    }

    @Override
    protected void onCancelled() {

    }
}

Now the issue is that the onScroll method keep on calling. It doesn't stop even when the user not scrolling. Can anyone have a solution ?

Upvotes: 14

Views: 3635

Answers (1)

Scorchio
Scorchio

Reputation: 2871

Please check the answer for this question: onScroll gets called when I set listView.onScrollListener(this), but without any touch .

The same is true for the GridView, since it has AbsListView as superclass just as ListView does.

Upvotes: 3

Related Questions