如何知道单击了特定 ListView 项目中的哪个视图
我有一个 ListView
,其中包含派生自 BaseAdapter
的我自己的自定义适配器。 ListView
中的每个项目都有 ImageView
和 TextView
等子项目。
我如何知道用户单击了这些子项中的哪一项?例如,是否可以在 getView
函数中附加一个侦听器,或者这可能是一个问题?
/ Henrik
编辑:目前我在 Activity 中有一个 onItemClick
,其中包含 ListView
。有没有什么好方法可以通过检查onItemClick
中的参数来知道ListView中特定项目中的哪个子项目已被按下。
@Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
.
.
}
I'm having a ListView
with my own custom adapter derived from a BaseAdapter
. Each item in the ListView
has sub items such as ImageView
and TextView
.
How can I know which one of these sub items the user clicked? Is it possible to attach a listener in the getView
function for example, or could that be a problem?
/ Henrik
Edit: Currently I have a onItemClick
in the Activity which contains the ListView
. Is there any good way to know which sub item in a specific item in the ListView which has been pressed by checking the params in the onItemClick
.
@Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
.
.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我使用了 Miga 的爱好编程 中的一个想法。
关键是从新的 onClick 侦听器调用 PerformItemClick()。这会将点击传递给已用于列表视图的 onItemClick()。它是如此的快速和简单,我觉得我在作弊。
这是来自列表适配器的 getView():
和 onItemClick():
I used an idea from Miga's Hobby Programming.
The key is calling performItemClick() from the new onClick listener. This passes the click on through to the onItemClick() that's already being used for the listview. It's so quick and easy, I feel like I'm cheating.
Here's getView(), from the list adapter:
And the onItemClick():
你可以做到的。您需要修改您的 getView 方法:
You can do it. You need to modify your getView method:
ListView 回收行视图对象,并在调用“getView”时为其分配新数据,因此使用的方法是在 getView 函数中添加侦听器。以下是应用程序中的代码示例,展示了如何完成此操作:
ListView recycles the row view objects and assigns fresh data on them when "getView" is called, so the approach to use, is to add a listener in the getView function. Here is a code sample from an app that shows how that is done:
由于您只有一个 ImageView 和一个 TextView,因此您可以将 ImageView 更改为 ImageButton。然后,您可以在 ImageButton 上添加一个侦听器,当用户单击图像时将调用该侦听器。如果他单击该项目内的任何其他位置(包括 TextView),则会调用 onItemclicklistener。
我认为这要简单得多。
Since you have only an ImageView and a TextView, you can change your ImageView to ImageButton. You can then add a listener on the ImageButton that will be called if the user clicks on the image. If he clicks anywhere else within the item (including the TextView), the onItemclicklistener will be called.
This is much simpler i think.
我想出了一个通用且可重用的解决方案。我没有扩展具体的列表适配器并修改
getView()
方法,而是创建了一个实现ListAdapter
接口的新类,该类盲目地将几乎所有内容转发到另一个ListAdapter< /code> 除了
getView()
。它看起来像这样:其他实现的方法如下所示:
要使用它,只需实例化它并提供任何其他
ListAdapter
(无论是ArrayAdapter
还是SimpleCursorAdapter
或其他任何内容)。然后为您想要侦听点击的每个视图调用setOnClickListener()
,在id
参数中提供其 id,并在listener
中提供您的侦听器代码>参数。要获取被单击的行的行 ID,请调用 ListView 的 getItemIdAtPosition(position) 方法(您必须通过其他方式获取该方法,因为它没有作为回调的参数给出,但是在大多数情况下这不应该是一个大问题)。该解决方案的优点是它可以与任何
ListAdapter
一起使用。因此,如果您的应用程序有多个ListView
,每个 ListView 使用不同的基础视图,甚至不同的适配器,则不必为每个列表创建一个新的适配器类。此问题与所有其他解决方案相同:如果您单击已注册的视图,则不会调用
ListView
的OnItemClick()
听众.对于未注册侦听器的视图,将调用此回调。因此,例如,您的列表项有一个活动,其中包含两个文本字段和一个按钮,并且您为该按钮注册了一个侦听器,然后单击该按钮将不会调用OnItemClick()
ListView 的 >,而是您的回调。单击其他任何位置都会调用OnItemClick()
。I came up with a generic and reusable solution. Instead of extending a concrete list adapter and modifying the
getView()
method, I created a new class implementing theListAdapter
interface that blindly forwards almost everything to anotherListAdapter
exceptgetView()
. It looks like this:The other implemented methods simply look like the following one:
To use it, simply instantiate it supplying any other
ListAdapter
(be itArrayAdapter
orSimpleCursorAdapter
or anything else). Then callsetOnClickListener()
for each view where you want to listen on the clicks, giving its id in theid
parameter, and your listener in thelistener
parameter. To get the row id for the row which was clicked, call thegetItemIdAtPosition(position)
method of your ListView (which you have to get some other way, because it's not given as parameter to your callback, but that shouldn't be a big problem in most cases).The advantage of this solution is that it can be used with any
ListAdapter
. So if your application has severalListView
s, each using different underlying views, or even different adapters, you don't have to create a new adapter class for each one.The problem with this is the same as with all the other solutions: the
OnItemClick()
of theListView
won't be called if you click on a view that you registered a listener for. For views that you didn't register a listener, this callback will be called though. So, for example, you have an activity for your list item that contains two text fields and a button, and you register a listener for the button, then clicking on the button won't call theOnItemClick()
of theListView
, but your callback instead. Clicking on anywhere else callsOnItemClick()
.