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())) 
相關問題