2

多くの要素を含む 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++;

        }
4

3 に答える 3

11

リストビューで必要なもののほとんどを処理するために、以前にこれらのヘルパー関数を少し異なる状態で使用しました。

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()))
}
于 2013-02-01T07:57:35.763 に答える
1

現在実装されているコードは、ループを制御してクリックを処理するときに、可視リスト項目のみを考慮しているようです。次の 2 つの動作に注意することが重要です。

まず、Android にはビュー リサイクルと呼ばれる概念があり、ListView を処理するときにメモリを節約するのに役立ちます。現在画面に表示されているビューのみが作成され、画面からスクロールすると、新しいデータが再入力されます。getChildCountしたがって、ListViewで やのようなメソッドを呼び出すとgetChildAt、これらの操作は表示されているアイテムに対してのみ実行されます。リストに入力されるデータに関する情報を検索するには、ListView のアダプターでgetCount()またはなどのメソッドを呼び出します。getItem()

第 2 に、このclickInList()メソッドは、リストの現在の位置を基準にして 1 からインデックス付けされ、表示されているアイテムに対してのみ使用できます。私の知る限り、リストが自動的にスクロールされることはありません。つまりclickInList(2)、リストの一番上にあるときに呼び出すと 2 番目の項目をクリックしますが、30 番目の項目がリストの一番上にあるときにもう一度呼び出すとclickInList(2)、32 番目の項目をクリックします。

これら 2 つのことを知った上で、ソリューションではすべてのリスト データを考慮する必要があり、クリックを行う際にもう少し精度が高くなる可能性があります。リストのすべての項目を確実に処理できるように 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();
}
于 2013-01-31T16:34:44.783 に答える
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);
        }
    });
}
于 2013-01-31T17:35:01.877 に答える