我试图单击“回收者”视图内的菜单项,我有以下代码。

onView(allOf(withId(R.id.section_list),isDisplayed())).perform(RecyclerViewActions.actionOnItem(hasDescendant(withText("ABC")), MyViewAction.clickChildViewWithId(R.id.payment_menu)));


MyViewAction看起来像这样:

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



测试时,我可以看到滚动发生在正确的位置,但是单击失败并出现以下错误



android.support.test.espresso.PerformException:在视图“ ID为com.em3Agri.operation.debug:id / section_list”的视图上执行“ android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction@2fd64b56”时出错。
在android.support.test.espresso.PerformException $ Builder.build(PerformException.java:83)
在 .........
原因:java.lang.IllegalStateException:位置38上没有视图持有者
在android.support.test.espresso.contrib.RecyclerViewActions $ ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:290)
在android.support.test.espresso.ViewInteraction $ 1.run(ViewInteraction.java:144)

最佳答案

怎么样:

onView(withId(R.id.section_list))
  .perform(
    RecyclerViewActions.actionOnItem(
      hasDescendant(withId(R.id.payment_menu)),
      ViewActions.click()
    )
  );

关于android - 在“回收者” View 中单击:Espresso,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39710805/

10-14 00:02