如何单击 Espresso 中搜索视图建议列表中的第一项?

How can I click the first item in searchview suggestion list in Espresso?

我有一个搜索视图,当我输入 "Example" 作为关键字时,它会显示一个下拉建议列表,其中包含 2 个带有文本 "Example" 的项目。我的意思是出于某种原因,建议列表中有 2 个同名的项目。

当我执行以下代码时:

onView(withText("Example"))
.inRoot(withDecorView(not(is(mActivityRuleSaves.getActivity().getWindow().getDecorView()))))
.perform(click());

它会给我例外,因为列表中有 2 个项目名称为 "Example"。

所以我的问题是如何选择并点击第一个?

希望你已经想通了?如果不是,那么我使用 onData 而不是 onView:

onData(anything()).atPosition(0).check(matches(hasDescendant(withText(("Blah")))));
onData(anything()).atPosition(1).check(matches(hasDescendant(withText("Blah")))).perform(click());

希望对您有所帮助。