2
我有一個包含EditText的視圖,我想確保它們按正確的順序顯示。我使用濃咖啡,但我不知道如何通過索引獲得視圖的孩子。這是一種方法嗎?意大利濃咖啡:如何通過索引獲得視圖的孩子
我有一個包含EditText的視圖,我想確保它們按正確的順序顯示。我使用濃咖啡,但我不知道如何通過索引獲得視圖的孩子。這是一種方法嗎?意大利濃咖啡:如何通過索引獲得視圖的孩子
我終於找到了答案here。 這個想法是創建一個新的匹配器。 這是一個更好的方式來做到這一點,直接與Espresso沒有創建一個匹配器?
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);
}
};
}