问题描述
您好我有一个使用回收站视图来显示一堆项目的应用程序。现在我想运行列表中的Android测试,但我不知道如何设置一个特定项目的纲领性点击。谁能告诉我如何实现这一目标?
Hi I have a app that uses a Recycler view to display a bunch of items. Now I want to run an android test on the list but I don't know how to set a programmatic click on a given item. Can anyone tell me how to achieve this?
推荐答案
您必须从头开始实现它。该RecyclerView缺少一些的一个ListView提供了默认情况下,真棒功能。鉴于此,在适配器必须声明一个接口,用于片段/活动和RecyclerView之间的观察员。
You have to implement it from scratch. The RecyclerView lacks for some of the awesome features that a ListView provides by default. Given this, in the adapter you have to declare an interface for the Observer between Fragment/Activity and the RecyclerView.
附上这个单击事件正确的方法是 onBindViewHolder
。然后在适配器您保持参照 CustomListener
或View.OnClickListener。
The correct method to attach this click event is onBindViewHolder
. Then in the adapter you keep the reference to the CustomListener
or the View.OnClickListener.
下面是如何做到这一点一个简单的例子:
Here is a quick example on how to do this:
@Override
public void onBindViewHolder(SearchListAdapter.ViewHolder holder, final int position) {
//Item clicked
holder.mParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Select or deselect
mListener.notify(holder, position);
}
});
}
而ViewHolder应该是这样的:
And the ViewHolder should be like:
public class ViewHolder extends RecyclerView.ViewHolder {
private View mParent;
public ViewHolder(View itemView) {
super(itemView);
mParent = itemView;
}
}
其中, mParent
是当前行查看。
这篇关于如何在一个RecyclerView一个项目进行编程点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!