我正在尝试使用相同的
来获取元素的数量此处How to get count of items with same ids which are not in adapter view的解决方案对我没有帮助。
static int counter = 0;
public static Matcher<View> withIdAndDisplayed(final int id) {
Checks.checkNotNull(id);
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("with item id: " + id);
}
@Override
public boolean matchesSafely(View view) {
if ((view.getId() == id) && (view.getGlobalVisibleRect(new Rect())
&& withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE).matches(view))){
counter++;
return true;
}
return false;
}
};
}
答案 0 :(得分:0)
<强>更新强>
我看到你试图获得不是adapterView的子视图计数。 请参阅此https://groups.google.com/forum/#!topic/android-test-kit-discuss/avLaBnBWr70
原始回答:
您使用的是RecyclerView吗?
我在测试中使用了以下代码来获取RecyclerView大小。
public static Matcher<View> withRecyclerViewSize(final int size) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(final View view) {
final int actualListSize = ((RecyclerView) view).getAdapter().getItemCount();
LOGD(TAG, "RecyclerView actual size " + actualListSize);
return actualListSize == size;
}
@Override
public void describeTo(final Description description) {
description.appendText("RecyclerView should have " + size + " items");
}
};
}
用法:onView(withId(R.id.resource_id)).check(matches(withRecyclerViewSize(expectedSize)));
在这种情况下,resource_id
是RecyclerView。
这里的例子很少:https://gist.github.com/chemouna/00b10369eb1d5b00401b。