如何计算ListView中具有相同ID的子项

时间:2016-09-15 22:47:13

标签: android android-studio android-testing android-espresso

我有一个ListView,它有多个Linearlayout。                 (1)的LinearLayout                       - >的FrameLayout                            - >的RelativeLayout                                   - > ImageView的                 (2)的LinearLayout                        - >的FrameLayout                             - >的RelativeLayout                                    - > ImageView的

如何计算ImageView的数量?所有人都有相同的身份

1 个答案:

答案 0 :(得分:0)

您可以使用自定义匹配器执行此操作 - 公共决赛班CustomMatchers {

public static Matcher<View> childAtPosition(
        final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}

}

在测试用例中使用它 -

ViewInteraction textView9 = onView(
                allOf(withText("ViewName"),
                        CustomMatchers.childAtPosition(
                          CustomMatchers.childAtPosition(withId(R.id.view)id), 1), 0), isDisplayed()))