Click all the list view elements while scrolling u

2019-01-24 23:41发布

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

        }

3条回答
家丑人穷心不美
2楼-- · 2019-01-25 00:28

It looks like your code, as currently implemented, is only considering the visibile list items when controlling the loop and handling the clicking. It's important to note the behavior of two things:

First, there's a concept called view recycling in Android that helps conserve memory when dealing with ListViews. Only the views that are currently on screen are created, and once they scroll off the screen they'll be repopulated with new data. Therefore, calling methods like getChildCount and getChildAt on a ListView will only perform these operations on the visible items. To find information about the data that populates the list, you can call methods such as getCount() or getItem() on the ListView's adapter.

Second, the clickInList() method is 1-indexed, relative to the current position of the list, and can only be used for visible items. As far as I know, it will never scroll your list automatically. This means that calling clickInList(2) when at the top of the list will click the second item, but then calling clickInList(2) again when the 30th item is at the top of the list will click the 32nd.

Knowing these two things, your solution will need to consider all of the list data and perhaps have a bit more precision when making clicks. Here's how I'd rewrite your while loop to ensure you'll be able to handle every item on the list, hope this helps:

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();
}
查看更多
SAY GOODBYE
3楼-- · 2019-01-25 00:33

It should help you(not tested):

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);
        }
    });
}
查看更多
成全新的幸福
4楼-- · 2019-01-25 00:34

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()))
}
查看更多
登录 后发表回答