在活动中定义RecyclerView的onCLickListener [英] Defining a RecyclerView's onCLickListener in an Activity
问题描述
RecyclerView与ListView有所不同,因为它不提供onItemCLickListener类来处理单击事件.
A RecyclerView is different from a ListView because it doesn't offer an onItemCLickListener class to handle click events.
通过单击ViewHolder中的onClickListener,可以在单击某个项目后在幕后发生很多事情的情况下解决该问题.
This can be tackled if not a lot is happening behind the scenes when an item is clicked, by defining an onClickListener in the ViewHolder.
如果ViewHolder可以访问的视图中确实不存在文本或必须传递给下一个Activity的内容,而是属于具有RecyclerView的Activity的一部分,怎么办?
What if the text or whatever that has to be passed to the next Activity doesnt really exist in the views that the ViewHolder has access to, but is rather a part of the Activity which has the RecyclerView?
在这种情况下,必须在活动内部实现onClickListener,以便可以转发文本.有可能.
In that case, the onClickListener must be implemented inside the activity so that the text can be forwarded. It is possible.
-
一种方法是添加一个包含该文本的不可见视图,然后执行之前执行的操作;在适配器中实现onClickListener.
One way is to add an Invisible View holding that text, and then doing what has been done before; implementing onClickListener in the Adapter.
以某种方式将该文本传递到适配器.
Pass that text, somehow, to the Adapter.
"2"怎么可能.被实施?
How can "2." be implemented?
推荐答案
在您的活动中编写一个公共函数,例如:
Write a public function in your activity like:
public void onClickCalled(String anyValue) {
// Call another acitivty here and pass some arguments to it.
}
现在是适配器的onClick功能
Now in your adapter's onClick funciton
@Override
public void onClick(View view) {
((YouActivityClass) context).onClickCalled("your argument here");
}
这篇关于在活动中定义RecyclerView的onCLickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!