Skip to content

Instantly share code, notes, and snippets.

@ikew0ng
Created June 6, 2014 08:04
Show Gist options
  • Save ikew0ng/3eb7504289b9017b73ba to your computer and use it in GitHub Desktop.
Save ikew0ng/3eb7504289b9017b73ba to your computer and use it in GitHub Desktop.
Smoothly scroll listView to specified position.
public class ListViewUtils {
public static final int SCROLL_DURATION = 150;
private ListViewUtils() {}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void scrollListView(final ListView listView, final int position) {
if (CommonUtils.hasHoneycomb()) {
listView.smoothScrollToPositionFromTop(position, 0);
listView.postDelayed(new Runnable() {
@Override
public void run() {
// Mock touchEvent to stop listView Scrolling.
listView.onTouchEvent(MotionEvent.obtain(System.currentTimeMillis(),
System.currentTimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
}
}, SCROLL_DURATION - 20);
listView.postDelayed(new Runnable() {
@Override
public void run() {
listView.setSelectionFromTop(position, 0);
}
}, SCROLL_DURATION);
} else {
listView.setSelectionFromTop(position, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment