问题描述
我正在尝试获取相同元素的数量
I am trying to get the count of elements with same rid
这里的解决方案 如何获取不在适配器视图中的具有相同 ID 的项目的数量 对我没有帮助.
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;
}
};
}
推荐答案
更新:
我看到您试图获取不是适配器视图的子视图计数.看到这个 https://groups.google.com/论坛/#!topic/android-test-kit-discuss/avLaBnBWr70
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
原答案:
你在使用 RecyclerView 吗?
Are you using RecyclerView?
我在测试中使用了以下代码来获取 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");
}
};
}
用法:onView(withId(R.id.resource_id)).check(matches(withRecyclerViewSize(expectedSize)));
在这种情况下,resource_id
是 RecyclerView.
In this case resource_id
is RecyclerView.
这里有几个例子:https://gist.github.com/chemouna/00b10369eb1d5b00401b.
这篇关于android espresso:不在适配器视图中的元素总数相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!