Click all the list view elements while scrolling using robotium

后端 未结 3 689
情深已故
情深已故 2020-12-17 06:58

I have a listView that contains lots of elements i.e. we have to scroll down to see all the elements. Now what i want to do is, click all the listView elements. How can I do

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 07:43

    I have previously used these helper functions in a slightly different state to handle most of what we need with listviews:

    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  void scrollListTo(final T listView,
            final int index, Instrumentation instrumentation) {
        instrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                listView.setSelection(index);
            }
        });
        instrumentation.waitForIdleSync();
    }
    

    With these your method would be:

    ListView list = solo.getCurrentListViews().get(0);
    for(int i=0; i < list.getAdapter().getCount(); i++){
        solo.clickOnView(getViewAtIndex(list, i, getInstrumentation()))
    }
    

提交回复
热议问题