在 Activity 中定义 RecyclerView 的 onCLickListener [英] Defining a RecyclerView's onCLickListener in an Activity

查看:15
本文介绍了在 Activity 中定义 RecyclerView 的 onCLickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

如果文本或任何必须传递给下一个 Activity 的内容实际上并不存在于 ViewHolder 可以访问的视图中,而是包含 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?

在这种情况下,必须在 Activity 内部实现 onClickListener,以便可以转发文本.这是可能的.

In that case, the onClickListener must be implemented inside the activity so that the text can be forwarded. It is possible.

  1. 一种方法是添加一个包含该文本的 Invisible View,然后做之前做过的事情;在适配器中实现 onClickListener.

  1. 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"怎么可能.实施?

推荐答案

在您的 Activity 中编写一个公共函数,例如:

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");
}

这篇关于在 Activity 中定义 RecyclerView 的 onCLickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆