我还看到了如下代码块:
public static class RecyclerViewItemCountAssertion implements ViewAssertion { private final int expectedCount;
public RecyclerViewItemCountAssertion(int expectedCount) {
this.expectedCount = expectedCount;
}
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.Adapter adapter = recyclerView.getAdapter();
assertThat(adapter.getItemCount(), is(expectedCount));
}
}
但我不确定如何操纵来获得计数
在你的测试中:
final View viewById = activityTestRule.getActivity().findViewById(R.id.your_recycler_view_id);
final RecyclerView recyclerView = (RecyclerView) viewById;
final RecyclerView.Adapter adapter = recyclerView.getAdapter();
final int itemsCount = adapter.getItemCount();
因此可以从0
迭代到itemscount-1
您可以使用该类在位置上获取回收器视图元素(在您的例子中0到itemscount-1
)
public class RecyclerViewMatcher {
private final int recyclerViewId;
public RecyclerViewMatcher(int recyclerViewId) {
this.recyclerViewId = recyclerViewId;
}
public Matcher<View> atPosition(final int position) {
return atPositionOnView(position, -1);
}
public Matcher<View> atPositionOnView(final int position, final int targetViewId) {
return new TypeSafeMatcher<View>() {
Resources resources = null;
View childView;
public void describeTo(Description description) {
String idDescription = Integer.toString(recyclerViewId);
if (this.resources != null) {
try {
idDescription = this.resources.getResourceName(recyclerViewId);
} catch (Resources.NotFoundException var4) {
idDescription = String.format("%s (resource name not found)", recyclerViewId);
}
}
description.appendText("with id: " + idDescription);
}
public boolean matchesSafely(View view) {
this.resources = view.getResources();
if (childView == null) {
RecyclerView recyclerView =
(RecyclerView) view.getRootView().findViewById(recyclerViewId);
if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
} else {
return false;
}
}
if (targetViewId == -1) {
return view == childView;
} else {
View targetView = childView.findViewById(targetViewId);
return view == targetView;
}
}
};
}}
onView(new RecyclerViewMatcher(R.id.your_recycler_view_id)
.atPositionOnView(0, R.id.your_item_body))
.perform(click());
这是我的项的代码。问题是,每个项目都显示在一整页的应用程序中。请帮我解决这个问题:
公共类DbHelper扩展了SQLiteOpenHelper{ }
我正面临一个问题,我无法删除RecycerView中的一个项目。我试图在互联网上搜索,但无法看到任何解决方案,我遵循这篇文章Android Recycerview添加和删除项目,但我仍然无法删除下面选择的项目是我的适配器和碎片的代码 RecyclerViewAdapter.java
我正在构建一个定制的Java库。我把我的大部分“重复”代码都保存在那里,比如文件处理、字符串处理等。每次我想使用它们时,我都必须将该类复制并粘贴到我正在进行的其他项目中。有没有办法让这个自定义库类成为“依赖项”?我在用我的智能手机。
附注:第一次用拖放开发任何东西。 关于如何使用ListView实现此功能,有很多主题,例如:https://raw.githubusercontent.com/btownRippleman/furthestProgress/master/furthestProgress/src/com/anappforthat/android/languagelineup/dynamicListView.java