Android - Espresso - scrolling to a non-list View item

前端 未结 3 1108
Happy的楠姐
Happy的楠姐 2021-01-02 05:08

Is there a general approach for scrolling to non-list View items that are not yet visible on the screen?

Without any precautions, Espresso will indicate that \"No V

3条回答
  •  时光取名叫无心
    2021-01-02 06:03

    Code that worked for me is:

    ViewInteraction tabView = onView(allOf(
        childAtPosition(childAtPosition(withId(R.id.bottomControlTabView), 0), 1), 
        isDisplayed()));
    tabView.perform(click());
    tabView.perform(click());
    
    public static Matcher childAtPosition(final Matcher parentMatcher, 
                                                final int position) {
    
            return new TypeSafeMatcher() {
                @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));
                }
            };
        }
    

提交回复
热议问题