-1

我一直在尋找在咖啡樣品,但他們只用下面的方法如何找到孩子在咖啡一些項目位置recyclerview

actionOnItemAtPosition 

在大多數情況下,給予直接的方法,在一些位置上進行點擊,我們會需要在某個位置點擊多個子項目中的一個。像一個TextView,ImageView和複選框。

如果我的Recyclerview有50個項目,我沒有看到任何簡單的方法到達位置25的複選框。除了定義我自己的ViewAction實現,任何人都可以有任何簡單的解決方案。

回答

0

你可以這樣創建自己的withIndex匹配:

public static Matcher<View> withIndex(final Matcher<View> matcher, final int index) { 
    return new TypeSafeMatcher<View>() { 
     int currentIndex = 0; 

     @Override 
     public void describeTo(Description description) { 
      description.appendText("with index: "); 
      description.appendValue(index); 
      matcher.describeTo(description); 
     } 

     @Override 
     public boolean matchesSafely(View view) { 
      return matcher.matches(view) && currentIndex++ == index; 
     } 
    }; 
} 

使用例如

int indexToSelect = 4; 
onView(allOf(withIndex(withId(R.id.textViewToSelect), indexToSelect), isDescendantOfA(withId(R.id.customRecyclerView)))).perform(click());