8

I am trying to know when the user has scrolled to the top or bottom of the listview and he cannot scroll anymore.

Now i'm using OnScrollListener to know what listview items are visible.

    listview.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            }
        }

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

            if (totalItemCount - visibleItemCount == firstVisibleItem) {
                //last item visible
            }

            if (firstVisibleItem == 0) {
                //first item visible
            }
        }
    });
user1940676
  • 4,348
  • 9
  • 44
  • 73
  • Okay, so why did your solution not work out? What's not working? – FD_ May 22 '14 at 12:52
  • because it does not tell me what I need to know when the user has scrolled to the top or bottom of the list and he cannot scroll further. – user1940676 May 22 '14 at 12:55

5 Answers5

14

I have found a solution by checking the offset of the first or the last item, when the offset of those items is 0 then we have reached the bottom/top of the listview.

    listview.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem == 0) {
                // check if we reached the top or bottom of the list
                View v = listview.getChildAt(0);
                int offset = (v == null) ? 0 : v.getTop();
                if (offset == 0) {
                    // reached the top:
                    return;
                } 
            } else if (totalItemCount - visibleItemCount == firstVisibleItem){
                View v =  listview.getChildAt(totalItemCount-1);
                int offset = (v == null) ? 0 : v.getTop();
                if (offset == 0) {
                    // reached the bottom:
                    return;
                }
            }               
        }
    });
meh
  • 22,090
  • 8
  • 48
  • 58
user1940676
  • 4,348
  • 9
  • 44
  • 73
2

Try this way

 list.setOnScrollListener(new OnScrollListener() {

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

            }

        int mPosition=0;
        int mOffset=0;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
             int position = list.getFirstVisiblePosition();
               View v = list.getChildAt(0);
                int offset = (v == null) ? 0 : v.getTop();

                if (mPosition < position || (mPosition == position && mOffset < offset)){
                     // Scrolled up 

                } else {
                     // Scrolled down

                }
        }
    }); 
M D
  • 47,665
  • 9
  • 93
  • 114
  • thanks, i forgot about the offsets, but one small thing inside the else how do I know when I reached the bottom of the list? – user1940676 May 22 '14 at 13:04
  • @user1940676 go to this [http://stackoverflow.com/questions/5123675/find-out-if-listview-is-scrolled-to-the-bottom](http://stackoverflow.com/questions/5123675/find-out-if-listview-is-scrolled-to-the-bottom) – M D May 22 '14 at 13:10
0

adapter getView method call when scroll listview

check the position when your position is equal to data size list view so you are on last element of list

Pawan asati
  • 292
  • 2
  • 13
  • not exactly, the getview is called when the view is becoming visible and not when it reaches the top/bottom of the list. – user1940676 May 22 '14 at 13:07
0
@Override
public void onScrollChanged()
{

// We take the last son in the scrollview
View view = (View) scrollview.getChildAt(scrollview.getChildCount() - 1);
int diff = (view.getBottom() - (scrollview.getHeight() + scrollview.getScrollY()));
if (diff == 0 )
{
// bottom of the scrollview
}
}
Anandroid
  • 403
  • 4
  • 14
0

If you are using a custom adapter with your listview, then use and enjoy this answer:

https://stackoverflow.com/a/55350409/1845404

The adapter's getView method detects when the list has been scrolled to the last item. It also adds correction for the rare times when some earlier position is called even after the adapter has already rendered the last view.

gbenroscience
  • 994
  • 2
  • 10
  • 33