使用robotium滚动时单击所有列表视图元素

时间:2013-01-31 14:17:11

标签: android listview robotium

我有一个包含大量元素的listView,即我们必须向下滚动才能看到所有元素。现在我要做的是,单击所有listView元素。我怎样才能做到这一点。现在,我使用以下代码,但它不会自动滚动。请帮忙。

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++;

        }

3 个答案:

答案 0 :(得分:11)

我以前使用这些辅助函数的状态略有不同,以处理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();
}

使用这些方法将是:

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

答案 1 :(得分:1)

当前实现的代码看起来像是在控制循环和处理点击时只考虑可见性列表项。重要的是要注意两件事的行为:

首先,在Android中有一个名为视图回收的概念,它有助于在处理ListView时节省内存。仅创建当前在屏幕上的视图,并且一旦它们滚动屏幕,它们将被重新填充新数据。因此,在ListView上调用getChildCountgetChildAt等方法只会对可见项执行这些操作。要查找有关填充列表的数据的信息,可以在ListView的适配器上调用getCount()getItem()等方法。

其次,clickInList()方法相对于列表的当前位置是1索引的,并且只能用于可见项。据我所知,它永远不会自动滚动您的列表。这意味着在列表顶部调用clickInList(2)会点击第二个项目,但是当第30个项目位于列表顶部时再次调用clickInList(2)将会点击第32个。

了解这两件事后,您的解决方案需要考虑所有列表数据,并且在点击时可能会更加精确。这是我如何重写你的while循环以确保你能够处理列表中的每个项目,希望这有帮助:

ListAdapter adapter = l.getAdapter();
for(int i=0; i < adapter.getCount(); i++) 
{
    //Scroll down the list to make sure the current item is visible
    solo.scrollListToLine(l, i);

    //Here you need to figure out which view to click on.
    //Perhaps using adapter.getItem() to get the data for the current list item, so you know the text it is displaying.

    //Here you need to click the item!
    //Even though you're in a list view, you can use methods such as clickOnText(), which might be easier based on how your adapter is set up

    solo.goBack();
}

答案 2 :(得分:0)

它应该帮助你(未经测试):

public void clickAllElementsOnListView(int index) {
    ListView listView = solo.getCurrentListViews().get(index);
    count = listView.getAdapter() != null ? listView.getAdapter().getCount() : 0;
    for (int i = 0; i < count; i++) {
        scrollListToLine(listView, i);
        solo.clickInList(1, index);
        solo.goBack();
    }
}

protected void scrollListToLine(final ListView listView, final int line) {
    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            listView.setSelection(line);
        }
    });
}