How to click on an item inside a RecyclerView in Espresso

前端 未结 10 2296
逝去的感伤
逝去的感伤 2020-12-01 05:33

I have a RecyclerView (R.id.recyclerView) where each row has an image (R.id.row_image) and a TextView. I want to click on the image in the first row.
I\'ve tried to use

10条回答
  •  無奈伤痛
    2020-12-01 05:56

    You should use a Custom ViewAction:

    public void clickOnImageViewAtRow(int position) {
        onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition(position, new ClickOnImageView()));
    }
    
    public class ClickOnImageView implements ViewAction{
        ViewAction click = click();
    
        @Override
        public Matcher getConstraints() {
            return click.getConstraints();
        }
    
        @Override
        public String getDescription() {
            return " click on custom image view";
        }
    
        @Override
        public void perform(UiController uiController, View view) {
            click.perform(uiController, view.findViewById(R.id.imageView));
        }
    }
    

提交回复
热议问题