Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android smoothScrollTo not invoking onScrollStateChanged

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.

like image 298
tyler Avatar asked Sep 28 '11 15:09

tyler


1 Answers

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

}
});
like image 62
saulpower Avatar answered Oct 16 '22 17:10

saulpower