Zac*_*ach 6 android android-espresso
我想知道如何在回收器视图中找到特定项目,其中每次运行时项目的顺序是随机的.
假设我在Recycler视图中有4个项目,每个项目都由相同类型的视图持有者表示,其中包含文本视图.每个视图持有者/项目都应用唯一标题.对于这个例子,假设为了简单起见,标题是"A","B","C"和"D".
如果订单是随机的,我如何找到位置(然后点击)项目"A"?我知道如果订单没有改变我可以使用scrollToPosition RecyclerViewInteraction操作,但在这种情况下,订单可以并且将会改变.
有什么想法吗?
谢谢,扎克
Zac*_*ach 15
我能够做到以下工作:
Matcher<RecyclerView.ViewHolder> matcher = CustomMatcher.withTitle("A");
onView((withId(R.id.recycler_view))).perform(scrollToHolder(matcher), actionOnHolderItem(matcher, click()));
Run Code Online (Sandbox Code Playgroud)
在哪里CustomMatcher.withTitle:
public static Matcher<RecyclerView.ViewHolder> withTitle(final String title)
{
return new BoundedMatcher<RecyclerView.ViewHolder, CustomListAdapter.ItemViewHolder>(CustomListAdapter.ItemViewHolder.class)
{
@Override
protected boolean matchesSafely(CustomListAdapter.ItemViewHolder item)
{
return item.mTitleView.getText().toString().equalsIgnoreCase(title);
}
@Override
public void describeTo(Description description)
{
description.appendText("view holder with title: " + title);
}
};
}
Run Code Online (Sandbox Code Playgroud)
您不需要为此使用自定义匹配器,只需使用它
onView(withId(R.id.recycler_id)).perform(RecyclerViewActions.actionOnItem(hasDescendant(withText("A")), click()));
Run Code Online (Sandbox Code Playgroud)
并添加 espresso-contrib 依赖项
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:<espressso-version>'
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的是 AndroidX
androidTestImplementation 'androidx.test.espresso:espresso-contrib:<espressso-version>'
Run Code Online (Sandbox Code Playgroud)