简体   繁体   中英

How to work with Android ListView setOnScrollListener?

I'm trying to increase the value of an integer number on scrolling at the end of list. If I have 5 numbers and scrolling at the end of last item in list-view that time I have to checked and scroll up to the 5 times till the condition is satisfied.

Here is my code

listView.setOnScrollListener(new AbsListView.OnScrollListener()
            {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {}

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

                    lastInScreen = firstVisibleItem + visibleItemCount;
                    //Log.e("","lastInScreen="+lastInScreen);
                    if(lastInScreen == totalItemCount)
                    {
                        Toast.makeText(getApplicationContext(), "Your Last Item." + lastInScreen , Toast.LENGTH_SHORT).show();
                        if(num < 5)
                        {
                            num += 1;

                            Log.e("","num = "+num );
                        }
                        if(num  == 5)
                        {
                            Log.e(""," Stop Scrolling!!! ");
                        }
                    }
                }
            });

But the problem is when I scroll first time at the end of listview item at that time num is incremented up to 5 at time like for loop and I don't want to this. I want if I scroll first num is incremented by 1, if I scroll again, num is incremented by 2 as it is up to a maxiumum of 5.

Try saving the "firstVisibleitem" and, if it's the same, don't do the sum.

int X=-1;
listView.setOnScrollListener(new AbsListView.OnScrollListener()
            {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {}

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                                     int visibleItemCount, int totalItemCount)
                {
                    lastInScreen = firstVisibleItem + visibleItemCount;
                    //Compare X with the firstVisibleItem (if X is -1, it always go for false, if X is not -1 X will be the last first visible item
                    if(X!=firstVisibleItem)
                    {
                        if(lastInScreen == totalItemCount)
                        {
                            Toast.makeText(getApplicationContext(), "Your Last Item." + lastInScreen , Toast.LENGTH_SHORT).show();
                            if(num < 5)
                            {
                                num += 1;
                                Log.e("","num = "+num );
                            }
                            if(num  == 5)
                            {
                                Log.e(""," Stop Scrolling!!! ");
                            }
                        }
                    }
                    //Set the value of this firstVisibleItem to X
                    X=firstVisibleItem;
                }
            });

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