Espresso:单击 ViewPager 中 ListView 中的第一项

Espresso: click first item in ListView inside a ViewPager

是否可以在 ViewPager 中单击 ListView 的第一项?

我已经尝试让它像这样工作:

onData(anything()).inAdapterView(withId(R.id.myListView)).atPosition(0).perform(click());.

但据我了解: 您不能将 onData 用于 ViewPager。 另外,如果我 运行 测试,我会得到多个匹配项的 AmbiguousViewMatcherException。

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: my.awesome.app:id/myListView' matches multiple views in the hierarchy. Problem views are marked with '****MATCHES****' below.

如果我 运行 这样的测试 onView(allOf(withId(R.id.myListView),isDisplayed())).perform(click()); 第一项永远不会被点击。

是否有其他方法可以单击列表的第一项?

试试这个:

onData(anything())
    .inAdapterView(allOf(withId(R.id.myListView), isCompletelyDisplayed()))
    .atPosition(0).perform(click());

因为你有一个 AmbiguousViewMatcherException,如果我没记错的话,这可能会匹配屏幕上显示的 "myListView"。