2016-07-12 24 views
0

與samerid元​​素的總數,我試圖讓元素的個數與同樣擺脫Android的咖啡:沒有適配器視圖

的這裏How to get count of items with same ids which are not in adapter view並沒有幫助我的解決方案。

static int counter = 0; 
public static Matcher<View> withIdAndDisplayed(final int id) { 
    Checks.checkNotNull(id); 
    return new TypeSafeMatcher<View>() { 
     @Override 
     public void describeTo(Description description) { 
      description.appendText("with item id: " + id); 
     } 

     @Override 
     public boolean matchesSafely(View view) { 
      if ((view.getId() == id) && (view.getGlobalVisibleRect(new Rect()) 
        && withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE).matches(view))){ 
       counter++; 
       return true; 
      } 
      return false; 
     } 
    }; 
} 

回答

0

更新:

我看你試圖讓這不是一個AdapterView孩子的觀看次數。 看到這個一個https://groups.google.com/forum/#!topic/android-test-kit-discuss/avLaBnBWr70

原來的答覆:

是否使用RecyclerView?

我在測試中使用下面的代碼來獲取RecyclerView大小。

public static Matcher<View> withRecyclerViewSize(final int size) { 
    return new TypeSafeMatcher<View>() { 

     @Override 
     public boolean matchesSafely(final View view) { 
      final int actualListSize = ((RecyclerView) view).getAdapter().getItemCount(); 
      LOGD(TAG, "RecyclerView actual size " + actualListSize); 
      return actualListSize == size; 
     } 

     @Override 
     public void describeTo(final Description description) { 
      description.appendText("RecyclerView should have " + size + " items"); 
     } 
    }; 
} 

用法:onView(withId(R.id.resource_id)).check(matches(withRecyclerViewSize(expectedSize)));

在這種情況下是resource_id RecyclerView。

這裏有幾個例子:https://gist.github.com/chemouna/00b10369eb1d5b00401b