Gridview onScroll method gets called always, without user scroll
Asked Answered
S

1

14

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 ?

Skimpy answered 13/5, 2013 at 8:55 Comment(0)
E
3

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.

Exuviate answered 31/7, 2013 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.