Android Espresso:如何通过索引获取视图的子级

Android Espresso:如何通过索引获取视图的子级,android,android-espresso,Android,Android Espresso,我有一个包含EditText的视图,我希望确保它们以正确的顺序显示。我使用浓缩咖啡,但我不知道如何通过索引获取视图的子视图。这是一种方法吗?我终于找到了答案。 其想法是创建一个新的匹配器。 这是一种更好的方法吗,直接用浓缩咖啡,而不用制作一个火柴 public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) { return ne

我有一个包含EditText的视图,我希望确保它们以正确的顺序显示。我使用浓缩咖啡,但我不知道如何通过索引获取视图的子视图。这是一种方法吗?

我终于找到了答案。 其想法是创建一个新的匹配器。 这是一种更好的方法吗,直接用浓缩咖啡,而不用制作一个火柴

public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) {
    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("position " + childPosition + " of parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            if (!(view.getParent() instanceof ViewGroup)) return false;
            ViewGroup parent = (ViewGroup) view.getParent();

            return parentMatcher.matches(parent)
                    && parent.getChildCount() > childPosition
                    && parent.getChildAt(childPosition).equals(view);
        }
    };
}
公共静态匹配器nthChildOf(最终匹配器parentMatcher,最终int childPosition){
返回新的TypeSafeMatcher(){
@凌驾
公共无效说明(说明){
说明.附录文本(“位置”+子位置+”父项);
parentMatcher.descripbeto(描述);
}
@凌驾
公共布尔匹配安全(视图){
如果(!(view.getParent()instanceof ViewGroup))返回false;
ViewGroup parent=(ViewGroup)view.getParent();
返回parentMatcher.matches(父项)
&&parent.getChildCount()>childPosition
&&parent.getChildAt(childPosition).equals(view);
}
};
}