我需要顺利滚动到某个位置,然后"跳"到另一个位置setSelection(anotherPosition).这样做是为了创造一个平滑滚动(例如)100个项目的幻觉ListView.smoothScrollToPosition(100)你知道,持续太久了.
setSelection()不等到smoothScrollToPosition完成它的工作,因此setSelection()被立即调用,用户只能看到快速跳跃;
private final int scrollableItems = 20;
int firstVisiblePosition = mListView.getFirstVisiblePosition();
if (firstVisiblePosition < scrollableItems) {
mListView.smoothScrollToPosition(0);
} else {
mListView.smoothScrollToPosition(firstVisiblePosition - scrollableItems);
mListView.setSelection(0);
}
mListView.clearFocus();
Run Code Online (Sandbox Code Playgroud)
好的,我们可以改变平滑幻觉的逻辑:首先setSelection(),然后顺利滚动(我们滚动到列表顶部的第一个项目):
int firstVisiblePosition = mListView.getFirstVisiblePosition();
if (firstVisiblePosition < scrollableItems) {
mListView.smoothScrollToPosition(0);
} else {
mListView.setSelection(scrollableItems);
mListView.smoothScrollToPosition(0);
}
mListView.clearFocus();
Run Code Online (Sandbox Code Playgroud)