Giorgio Vespucci
Giorgio Vespucci

Reputation: 1586

Android ListView: detecting if first/last row is visible after scroll is finished

I have to implement a sort of pagination for a subclass of ListView. When the user scrolls down the list, the scroll is finished and he views the last row, I have to request next page of data, if any. Same thing for scroll up/first row/previous page.

public class ContactList extends ListView implements OnGestureListener {

    GestureDetector gestureDetector;

    public ContactList(Context context) {
        super(context);
        gestureDetector = new GestureDetector(this);
    }

//Other ctors here...       
}

I then attached a GestureDetector to the ListView, forwarding to it all the touches.

public boolean onTouchEvent(MotionEvent ev) {
    return gestureDetector.onTouchEvent(ev);
}

Can I detect in my OnGestureListener.onScroll() if first/last row is visible using getFirst/LastVisiblePosition()?

Maybe this method is called before the scroll occurs?

Thank you for any help.

Upvotes: 2

Views: 4787

Answers (1)

GrAnd
GrAnd

Reputation: 10211

I think you need to setup OnScrollListener for your ListView and gather positions from onScroll callback:

    setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            // You cat determine first and last visible items here
            // final int lastVisibleItem = firstVisibleItem + visibleItemCount - 1;
        }

        @Override
        public void onScrollStateChanged(AbsListView arg0, int arg1) {
            // TODO Auto-generated method stub
        }
    });

Upvotes: 6

Related Questions