6

I am using smoothScrollBy() to scroll to a specific position in a ListView. I would like to be notified when the ListView is done scrolling to integrate it with the current onScrollStateChanged() event that is fired off when the user scrolls with their finger.

Currently I'm using a Timer that runs 100ms after the duration of the smooth scroll, but that is not as event driven as I would prefer.

1
  • 1
    I can confirm from my own debugging that the smoothScroll* functions won't trigger the scrolling state changes, and therefore render the OnScrollListener.onScrollStateChanged() callbacks useless.
    – ohhorob
    Commented Nov 23, 2011 at 19:17

1 Answer 1

2

If you have implemented the OnScrollListener for your listview you can watch for when the ScrollState changes. Use a global boolean (isScrolling) set to true when you call smoothScrollBy() and then set it to false once the OnScrollListener registers a ScrollState of SCROLL_STATE_IDLE.

sidebar1.smoothScrollToPositionFromTop(currentPosition, 0, 500);
isScrolling = true;

sidebar1.setOnScrollListener(new OnScrollListener() {

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

}

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

}
});
5
  • Unfortunately smoothScrollToPositionFromTop came in at API 11.
    – Tyler
    Commented Oct 6, 2011 at 18:37
  • True, but aren't you just looking to be notified when the scrolling is done? You should be able to use smoothScrollBy and implement the listener in conjunction with a while loop that continuously polls the isScrolling state.
    – saulpower
    Commented Oct 7, 2011 at 16:59
  • 2
    From my experience, smoothScrollBy() does not invoke onScrollStateChanged(), and as of this time I am using a Timer to poll once the scrolling should be done.
    – Tyler
    Commented Oct 7, 2011 at 17:58
  • Another thought would be to poll and check the getLastVisiblePosition() once that changes and then stays consistent you can probably be certain scrolling has ceased. It's not really event driven but should get the job done pretty well.
    – saulpower
    Commented Dec 2, 2011 at 17:31
  • On a Galaxy Tab 10.1 (Android 3.1) smoothScrollBy does trigger a call to onScrollStateChanged. Most likely, this also happens on a Huawei Media PadS7 - 301u (Android 3.2). On 2.x release, afaik, the smoothScrollBy does not trigger onScrollStateChanged Commented Dec 13, 2011 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.