简体   繁体   中英

Android GridView scrolling loop

I'm working with a gridview where I load data and I've set that when it scroll to the bottom start loading more data. The problems is that the grid enter by himself in a loop because when the first data is loaded or just added to the grid the second time. The grid detect it as the "bottom" and start again... I tried to add a bool for detecting when it's the first time or not, but the second one always start the loop.

Here is my scroll code:

androidGridView.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
            {
                if(firstVisibleItem + visibleItemCount >= totalItemCount){
                    EditText rl1 = (EditText) findViewById(R.id.editText);
                    new SearchTask().execute(rl1.getText().toString());
                }
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){

            }
        });

Thanks!

In your onScrollStateChanged you can detect if the user has scrolled or not. With this information you can prevent the issue you're facing. Here is an example:

        static boolean userScrolled = false;

        androidGridView.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
            {
                if(userScrolled == true && (firstVisibleItem + visibleItemCount >= totalItemCount)){
                    EditText rl1 = (EditText) findViewById(R.id.editText);
                    new SearchTask().execute(rl1.getText().toString());
                }
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){
                if(scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
                    userScrolled = true;
                }   
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM