Espresso父视图点击

时间:2016-11-14 18:28:40

标签: android expandablelistview android-espresso

 <LinearLayout [...]
    <ExpandableListView
        android:id="@+id/options"
        android:layout_width="match_parent"
        android:groupIndicator="@null"
        android:layout_height="wrap_content"
        android:background="@null"
        android:divider="@android:color/white"
        android:scrollbarSize="0dp"
        tools:visibility="gone" />
</LinearLayout>

如何使用espresso点击ExpandableListView上的孩子,其中&#34; blahblahblah&#34;是我的项目视图ID,如下例所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/blahblahblah"
android:orientation="vertical">

1 个答案:

答案 0 :(得分:1)

您应该使用Espresso.onData方法。具体方法取决于您希望如何指定要选择的ExpandableListView项目。

通常你会使用这两种方法中的一种:

1。在data

中匹配Adapter

假设您的ExpandableListAdapter数据基于MyData个对象:

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    List<MyData> data = new ArrayList();

    //... all the other stuff you need for it to work

}

您可以使用Espresso.onData来匹配所需的记录(例如,通过匹配其toString()方法):

onData(hasToString(containsString("example"))
        .inAdapterView(withId(R.id.options))
        .onChildView(withId(R.id.blablabla))
        .perform(click());

(特定toString()对象的MyData必须包含String "example"

2。在position

中匹配Adapter

无论您的子对象与哪个数据相关联,都要与position中的ListView匹配(在示例position == 0中):

onData(anything())
        .inAdapterView(withId(R.id.options))
        .atPosition(0)
        .onChildView(withId(R.id.blablabla))
        .perform(click());