为什么我的 onItemSelectedListener 没有在 ListView 中调用? [英] Why is my onItemSelectedListener not called in a ListView?

查看:11
本文介绍了为什么我的 onItemSelectedListener 没有在 ListView 中调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 ListView 设置如下:

I'm using a ListView that is setup like this:

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:longClickable="false"
    android:choiceMode="singleChoice">
</ListView>

在我的代码中,我将一个 OnItemSelectedListener 添加到 ListView,如下所示:

In my code I add an OnItemSelectedListener to the ListView like this:

getListView().setAdapter(adapter);
getListView().setOnItemSelectedListener(this);

我的 Activity 实现了这样的监听器:

my Activity implements the listener like that:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Log.d("Tag", "ListItemSelected: Parent: " + parent.toString() + " View: "
            + view.toString() + " Position: " + " Id: " + id);
}

我的希望是,当我单击列表中的某些内容时,我会看到此调试输出.但调试输出从未显示在 LogCat 中.

My hope was, that I would see this debug output the moment I click on something in the list. But the debug output is never shown in LogCat.

推荐答案

OnItemSelectedListener 监听列表项选择而不是列表项点击.

The OnItemSelectedListener listens for list item selections and not list item clicks.

在这种情况下,可以将选择视为使用设备的触控板将焦点移动到该项目上.

A selection in this case could be seen as moving the focus on this item with the device's trackpad.

要获得想要的行为,必须使用 OnItemClickListener.

To get the wanted behavior one must use the OnItemClickListener.

这篇关于为什么我的 onItemSelectedListener 没有在 ListView 中调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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