Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 921
hacker Avatar asked May 13 '13 08:05

hacker


1 Answers

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.

like image 142
Scorchio Avatar answered Sep 18 '22 19:09

Scorchio