浓缩咖啡 - 单击gridview内的图像

时间:2015-01-21 12:53:04

标签: android android-gridview android-espresso

我有一个如下所示的视图层次结构:

GridView{id=2131362110, res-name=item_list_grid, 
|
+----->RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
+----->RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
+------>RelativeLayout{id=2131362124, res-name=item_image_thumb_layout
|
+------------->ImageView{id=2131362125, res-name=item_image
|
GridView{id=2131362110, res-name=item_list_grid, ...etc

我想点击其中一个带有id = item_image的ImageView。

我不能像onPosition(x)一样使用onView,所以我使用了onData。我尝试了所有这些:

onData(allOf(anything(),withId(R.id.item_image))).atPosition(0).perform(click());

onData(anything()).atPosition(0).perform(click());

onData(allOf(atPosition(0),withId(R.id.item_image))).perform(click());

但所有结果都是

android.support.test.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy.

对此有何建议?谢谢!

1 个答案:

答案 0 :(得分:17)

您的错误消息告诉您,您的活动中有多个观看点已扩展为AdapterView,因此您的布局中还有另一个ListViewGridView

您可以选择数据层上的AdapterView。因此,请使用AdapterView

类型的项目选择此ItemModel
onData(is(instanceOf(ItemModel.class))).atPosition(0)
    .onChildView(withId(R.id.item_image)).perform(click());

或者您可以按ID

选择特定的AdapterView
onData(anything()).inAdapterView(withId(R.id.my_grid_view)).atPosition(0).
            onChildView(withId(R.id.item_image)).perform(click());