Android 浓缩咖啡:如何按索引滚动到水平滚动视图中的项目?
这就是我的Android 浓缩咖啡:如何按索引滚动到水平滚动视图中的项目?,android,android-espresso,horizontal-scrolling,horizontalscrollview,Android,Android Espresso,Horizontal Scrolling,Horizontalscrollview,这就是我的水平滚动视图的外观: <HorizontalScrollView android:layout_below="@id/saved_circuits_title" android:id="@+id/saved_circuits_scrollview" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayou
水平滚动视图
的外观:
<HorizontalScrollView
android:layout_below="@id/saved_circuits_title"
android:id="@+id/saved_circuits_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/saved_circuits_scroll"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
如何使用Espresso在HorizontalScrollView中的指定索引处滚动到ImageView并单击它
我试过的 我的布局xml中没有ID,因此以下方法不起作用:
onView( withId( R.id.button)).perform( scrollTo(), click());
我知道您可以并尝试为水平滚动视图找到类似的方法
:
onView(withId(R.id.saved_circuits_scroll))
.perform(HorizontalScrollViewActions.actionOnItemAtPosition(0, click()));
除了HorizontalScrollViewActions
不存在之外
或者,我尝试了以下操作,至少在指定索引处单击水平滚动视图
中的一个项目:
// Click item at position 3
onView(withHorizontalScrollView(R.id.scroll_view).atPosition(3)).perform(click());
// Convenience helper
public static HorizontalScrollViewMatcher withHorizontalScrollView(final int horizontalScrollViewId) {
return new HorizontalScrollViewMatcher(horizontalScrollId);
}
除了HorizontalScrollViewMatcher
不存在之外
如何处理水平滚动视图
?它不是ScrollView
的后代,因此答案建议我需要实现自己的自定义ViewAction
。我只想按索引滚动到水平滚动视图
中的一个项目,然后单击它。这真的需要吗?如果这就是我需要做的,我该如何实现这个定制的ViewAction
尝试添加此匹配器
public static Matcher<View> withIdAndParentId(final int viewId, final int parentId) {
Assert.assertTrue(viewId != -1);
Assert.assertTrue(parentId != -1);
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
}
@Override
public boolean matchesSafely(View view) {
return view.getId() == viewId && isThereParentWithIdInHierarchy(view);
}
private boolean isThereParentWithIdInHierarchy(View view) {
ViewParent viewParent = view.getParent();
if (viewParent == null || !(viewParent instanceof ViewGroup))
return false;
ViewGroup parent = (ViewGroup) viewParent;
return parent.getId() == parentId || isThereParentWithIdInHierarchy(parent);
}
};
}
希望有帮助。好吧,在我的特殊情况下,我发现我的滚动视图没有与之关联的ID(或者没有我可以合理访问的ID,因此我无法使用Mody的答案)。然而,他们确实有一个与他们相关联的标签,所以我可以用Espresso的ViewMatcher来代替。每个视图都与一个circuitProject对象关联(当然,对于您来说,它可能不同)。我可以访问以下内容:
ArrayList<CircuitProject> circuitProjects = new ArrayList<>();
在我的例子中,我通过使用:
onView(allOf(withId(R.id.itemTextView)、withEffectiveVisibility(Visibility.VISIBLE)、withText(R.string.categories))。执行(scrollTo(),click())
R.id.itemTextView
是动态添加到线性布局中的文本视图(带有文本R.string.categories
):
... [动态添加的子对象]
onView(withIdAndParentId(R.id.YOUR_PARTICULAR_VIEW_ID, R.id.horizontalScrollViewId)).perform(scrollTo(), click());
ArrayList<CircuitProject> circuitProjects = new ArrayList<>();
onView(withTagValue(withStringMatching(circuitProject.getFolderID()))).perform(scrollTo(), click());