1 つの方法は、既存のスクロール モーションを停止するSmoothScrollToPositionを使用することです。このメソッドには API レベル >= 8 (Android 2.2、Froyo) が必要であることに注意してください。
現在の位置が目的の位置から遠く離れている場合、スムーズなスクロールにはかなりの時間がかかり、少しぎこちなく見えることに注意してください (少なくとも Android 4.4 KitKat でのテストでは)。また、setSelection と SmoothScrollToPosition の呼び出しを組み合わせて使用すると、位置がわずかに「ミス」することがあることがわかりました。これは、現在の位置が目的の位置に非常に近い場合にのみ発生するようです。
私の場合、ユーザーがボタンを押したときにリストが一番上(位置 = 0) にジャンプするようにしました (これはユースケースとは少し異なるため、ニーズに合わせて調整する必要があります)。
私は次の方法を使用しました
private void smartScrollToPosition(ListView listView, int desiredPosition) {
// If we are far away from the desired position, jump closer and then smooth scroll
// Note: we implement this ourselves because smoothScrollToPositionFromTop
// requires API 11, and it is slow and janky if the scroll distance is large,
// and smoothScrollToPosition takes too long if the scroll distance is large.
// Jumping close and scrolling the remaining distance gives a good compromise.
int currentPosition = listView.getFirstVisiblePosition();
int maxScrollDistance = 10;
if (currentPosition - desiredPosition >= maxScrollDistance) {
listView.setSelection(desiredPosition + maxScrollDistance);
} else if (desiredPosition - currentPosition >= maxScrollDistance) {
listView.setSelection(desiredPosition - maxScrollDistance);
}
listView.smoothScrollToPosition(desiredPosition); // requires API 8
}
ボタンのアクションハンドラーで、次のようにこれを呼び出しました
case R.id.action_go_to_today:
ListView listView = (ListView) findViewById(R.id.lessonsListView);
smartScrollToPosition(listView, 0); // scroll to top
return true;
上記はあなたの質問に直接答えるものではありませんが、現在の位置が目的の位置またはその近くにあることを検出できる場合は、SmoothScrollToPositionを使用してスクロールを停止できます。