Using Espresso to click view inside RecyclerView item

后端 未结 9 1858
醉酒成梦
醉酒成梦 2020-12-02 05:36

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.i

相关标签:
9条回答
  • 2020-12-02 06:18

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

    Then you can click it with

    onView(withId(R.id.rv_conference_list)).perform(
                RecyclerViewActions.actionOnItemAtPosition(0, MyViewAction.clickChildViewWithId(R.id. bt_deliver)));
    
    0 讨论(0)
  • 2020-12-02 06:23

    Here is, how I resolved issue in kotlin:

    fun clickOnViewChild(viewId: Int) = object : ViewAction {
        override fun getConstraints() = null
    
        override fun getDescription() = "Click on a child view with specified id."
    
        override fun perform(uiController: UiController, view: View) = click().perform(uiController, view.findViewById<View>(viewId))
    }
    

    and then

    onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(position, clickOnViewChild(R.id.viewToClickInTheRow)))
    
    0 讨论(0)
  • 2020-12-02 06:28

    You can click on 3rd item of recyclerView Like this:

    onView(withId(R.id.recyclerView)).perform(
                    RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(2,click()))
    

    Do not forget to provide the ViewHolder type so that inference does not fail.

    0 讨论(0)
提交回复
热议问题