I am trying to get a ListView to realign after every scroll/fling. By realign I mean realign in such a way, that the top item of the ListView is aligned with the top of the ListView, if it is cut off it should scroll down smoothly until it is realigned and fully visible.
I implemented a scroll-listener:
firstRowListView.setOnScrollListener(new OnScrollListener() {
private boolean correcting = false;
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
@Override
public void onScrollStateChanged(AbsListView arg0, int scrollState) {
if (!correcting && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
correcting = true;
firstRowListView.smoothScrollToPosition(firstRowListView.getFirstVisiblePosition());
correcting = false;
}
}
}
});
(For easier visibility I only left the important bits in). If I scroll smoothly (no fling) it works fine, but if I fling the list it doesn't realign itself. Although LogCat tells me that the onScrollStateChange-method is executed in the same way as when I perform a "normal" scroll.
Why is this and how do I get the ListView to realign even after a Fling?