Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

android - Click all the list view elements while scrolling using robotium

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 that. Right now,I am using the following code but it doesn't scroll automatically. Please help.

ListView l = solo.getCurrentListViews().get(0);
        assertNotNull("No list views!", l);
        assertTrue("No items in list view!", l.getChildCount() > 0);
        // Get the last list item
        View v = l.getChildAt(l.getChildCount());
        System.out.println("getChildCount: " + l.getChildCount());

        int i = 1;
        while (i <= l.getChildCount()) {
            solo.clickInList(i);

            solo.goBack();

            i++;

        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 <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();
}

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()))
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...