0

我有一个ListView,它有多个Linearlayout。 (1)的LinearLayout - >的FrameLayout - >的RelativeLayout - >的ImageView (2)的LinearLayout - >的FrameLayout - >的RelativeLayout - >的ImageView如何在ListView中统计具有相同ID的孩子

我怎样才能计数的ImageView的数量?他们都具有相同的id

+0

检查:http://stackoverflow.com/questions/38318086/android-espresso-total-count-of-elements-with-samerid-not-in-adapter-view – piotrek1543

回答

0

您可以使用自定义匹配器做到这一点 - 公共final类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())) 
相关问题