当前位置: 首页 > 知识库问答 >
问题:

如何用Android Espresso点击RecycerView中的每一个项目

郭和硕
2023-03-14

我还看到了如下代码块:

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));
    }
}

但我不确定如何操纵来获得计数

共有1个答案

佘飞鸣
2023-03-14

在你的测试中:

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());
 类似资料: