使用 Espresso 单击 RecyclerView 项目内的视图 [英] Using Espresso to click view inside RecyclerView item

查看:28
本文介绍了使用 Espresso 单击 RecyclerView 项目内的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Espresso 单击 RecyclerView 项目内的特定视图?我知道我可以使用以下方法单击位置 0 处的项目:

How can I use Espresso to click a specific view inside a RecyclerView item? I know I can click the item at position 0 using:

onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

但我需要点击该项目内的特定视图,而不是该项目本身.

But I need to click on a specific view inside that item and not on the item itself.

提前致谢.

-- 编辑--

更准确地说:我有一个 RecyclerView (R.id.recycler_view),其中的项目是 CardView (R.id.card_view).在每个 CardView 中,我有四个按钮(除其他外),我想单击一个特定按钮 (R.id.bt_deliver).

To be more precise: I have a RecyclerView (R.id.recycler_view) which items are CardView (R.id.card_view). Inside each CardView I have four buttons (amongst other things) and I want to click on a specific button (R.id.bt_deliver).

我想使用 Espresso 2.0 的新功能,但我不确定这是否可行.

I would like to use the new features of Espresso 2.0, but I'm not sure that is possible.

如果不可能,我想使用这样的东西(使用 Thomas Keller 代码):

If not possible, I wanna use something like this (using Thomas Keller code):

onRecyclerItemView(R.id.card_view, ???, withId(R.id.bt_deliver)).perform(click());

但我不知道该在问号上加什么.

but I don't know what to put on the question marks.

推荐答案

您可以通过自定义视图操作来实现.

You can do it with customize view action.

public class MyViewAction {

    public static ViewAction clickChildViewWithId(final int id) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return null;
            }

            @Override
            public String getDescription() {
                return "Click on a child view with specified id.";
            }

            @Override
            public void perform(UiController uiController, View view) {
                View v = view.findViewById(id);
                v.performClick();
            }
        };
    }

}

然后你可以点击它

onView(withId(R.id.rv_conference_list)).perform(
            RecyclerViewActions.actionOnItemAtPosition(0, MyViewAction.clickChildViewWithId(R.id. bt_deliver)));

这篇关于使用 Espresso 单击 RecyclerView 项目内的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆