如何使用咖啡点击一个RecyclerView项目内部的具体看法? 我知道我可以在位置使用0单击该项目:
onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
但我需要点击该项目里面,而不是项目本身特定的视图。
提前致谢。
- 编辑 -
为了更精确:我有一个RecyclerView( R.id.recycler_view
)哪些项目是CardView( R.id.card_view
)。 每个CardView里面我有四个按钮(除其他事项外),我想点击一个特定的按钮( R.id.bt_deliver
)。
我想用咖啡2.0的新功能,但我不知道这是可能的。
如果没有可能,我想用这样的事情(使用托马斯凯勒代码):
onRecyclerItemView(R.id.card_view, ???, withId(R.id.bt_deliver)).perform(click());
但我不知道该穿什么的问号。
Answer 1:
您可以使用自定义视图操作做到这一点。
public class MyViewAction {
public static ViewAction clickChildViewWithId(final int id) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}
@Override
public String getDescription() {
return "Click on a child view with specified id.";
}
@Override
public void perform(UiController uiController, View view) {
View v = view.findViewById(id);
v.performClick();
}
};
}
}
然后,你可以点击它
onView(withId(R.id.rv_conference_list)).perform(
RecyclerViewActions.actionOnItemAtPosition(0, MyViewAction.clickChildViewWithId(R.id. bt_deliver)));
Answer 2:
现在,随着android.support.test.espresso.contrib它已经变得更加容易:
1)添加测试依赖
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
*排除3个模块,因为很可能你已经拥有了它
2)然后像做
onView(withId(R.id.recycler_grid))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
要么
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItem(
hasDescendant(withText("whatever")), click()));
要么
onView(withId(R.id.recycler_linear))
.check(matches(hasDescendant(withText("whatever"))));
Answer 3:
尝试下方法:
onView(withRecyclerView(R.id.recyclerView)
.atPositionOnView(position, R.id.bt_deliver))
.perform(click());
public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
return new RecyclerViewMatcher(recyclerViewId);
}
public class RecyclerViewMatcher {
final int mRecyclerViewId;
public RecyclerViewMatcher(int recyclerViewId) {
this.mRecyclerViewId = recyclerViewId;
}
public Matcher<View> atPosition(final int position) {
return atPositionOnView(position, -1);
}
public Matcher<View> atPositionOnView(final int position, final int targetViewId) {
return new TypeSafeMatcher<View>() {
Resources resources = null;
View childView;
public void describeTo(Description description) {
int id = targetViewId == -1 ? mRecyclerViewId : targetViewId;
String idDescription = Integer.toString(id);
if (this.resources != null) {
try {
idDescription = this.resources.getResourceName(id);
} catch (Resources.NotFoundException var4) {
idDescription = String.format("%s (resource name not found)", id);
}
}
description.appendText("with id: " + idDescription);
}
public boolean matchesSafely(View view) {
this.resources = view.getResources();
if (childView == null) {
RecyclerView recyclerView =
(RecyclerView) view.getRootView().findViewById(mRecyclerViewId);
if (recyclerView != null) {
childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
}
else {
return false;
}
}
if (targetViewId == -1) {
return view == childView;
} else {
View targetView = childView.findViewById(targetViewId);
return view == targetView;
}
}
};
}
}
Answer 4:
这里,我怎么在科特林解决的问题:
fun clickOnViewChild(viewId: Int) = object : ViewAction {
override fun getConstraints() = null
override fun getDescription() = "Click on a child view with specified id."
override fun perform(uiController: UiController, view: View) = click().perform(uiController, view.findViewById<View>(viewId))
}
然后
onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(position, clickOnViewChild(R.id.viewToClickInTheRow)))
Answer 5:
您可以点击 第三个项目 recyclerView
就像这样:
onView(withId(R.id.recyclerView)).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(2,click()))
不要忘了提供ViewHolder
类型,以便推断不会失败。
Answer 6:
首先给你的按钮独特contentDescriptions,即“交货按钮行5”。
<button android:contentDescription=".." />
然后滚动到行:
onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition(5));
然后选择基于contentDescription的看法。
onView(withContentDescription("delivery button row 5")).perform(click());
内容描述是用咖啡的onView,使您的应用程序更易于访问的好方法。
文章来源: Using Espresso to click view inside RecyclerView item