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.

Tyler
  • 19,113
  • 19
  • 94
  • 151
  • 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 Nov 23 '11 at 19:17

1 Answers1

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) {

}
});
saulpower
  • 1,258
  • 1
  • 10
  • 13
  • Unfortunately `smoothScrollToPositionFromTop` came in at API 11. – Tyler Oct 06 '11 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 Oct 07 '11 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 Oct 07 '11 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 Dec 02 '11 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 – Marc Van Daele Dec 13 '11 at 12:42