Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if the user has scrolled to the top or bottom of a listview/scrollview

Tags:

android

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
            }
        }
    });
like image 267
user1940676 Avatar asked May 22 '14 12:05

user1940676


People also ask

How do I check my scroll position in flutter?

I used NotificationListener that is a widget that listens for notifications bubbling up the tree. Then use ScrollEndNotification , which indicates that scrolling has stopped. For scroll position I used _scrollController that type is ScrollController .

Does ListView have scroll?

10 Answers. Show activity on this post. Never put ListView in ScrollView . ListView itself is scrollable.

What is the difference between ListView and ScrollView?

ScrollView is used to put different or same child views or layouts and the all can be scrolled. ListView is used to put same child view or layout as multiple items. All these items are also scrollable. Simply ScrollView is for both homogeneous and heterogeneous collection.

How do I make ListView scroll smoothly?

The key to a smoothly scrolling ListView is to keep the application's main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread. To test the status of your app, you can enable StrictMode .


2 Answers

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;
                }
            }               
        }
    });
like image 141
user1940676 Avatar answered Oct 06 '22 04:10

user1940676


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

                }
        }
    }); 
like image 22
M D Avatar answered Oct 06 '22 04:10

M D