I'm not sure exactly what you are trying to do. I assume that you have a list with too many elements to fit on the screen, and you want to press a button located at 10th position, or something in this effect? I'm right?
If so, I previously created some helper functions for listview to get a view at a given index in a list view:
public View getViewAtIndex(final ListView listElement, final int indexInList, Instrumentation instrumentation) { ListView parent = listElement; if (parent != null) { if (indexInList <= parent.getAdapter().getCount()) { scrollListTo(parent, indexInList, instrumentation); int indexToUse = indexInList - parent.getFirstVisiblePosition(); return parent.getChildAt(indexToUse); } } return null; } public <T extends AbsListView> void scrollListTo(final T listView, final int index, Instrumentation instrumentation) { instrumentation.runOnMainSync(new Runnable() { @Override public void run() { listView.setSelection(index); } }); instrumentation.waitForIdleSync(); }
source share