问题
I am trying to get the count of elements with same rid
The solutions here How to get count of items with same ids which are not in adapter view is not helping me.
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;
}
};
}
回答1:
Update:
I see you trying to get child view count which is not an adapterView. See this one https://groups.google.com/forum/#!topic/android-test-kit-discuss/avLaBnBWr70
Original answer:
Are you using RecyclerView?
I used below code in my tests to get the RecyclerView size.
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");
}
};
}
Usage: onView(withId(R.id.resource_id)).check(matches(withRecyclerViewSize(expectedSize)));
In this case resource_id
is RecyclerView.
There are few examples here: https://gist.github.com/chemouna/00b10369eb1d5b00401b.
来源:https://stackoverflow.com/questions/38318086/android-espresso-total-count-of-elements-with-samerid-not-in-adapter-view