Created
June 6, 2014 08:04
-
-
Save ikew0ng/3eb7504289b9017b73ba to your computer and use it in GitHub Desktop.
Smoothly scroll listView to specified position.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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