Android 为什么我的 onItemSelectedListener 没有在 ListView 中调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2454337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:55:11  来源:igfitidea点击:

Why is my onItemSelectedListener not called in a ListView?

androidandroid-listviewlistener

提问by Janusz

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

我正在使用一个像这样设置的 ListView:

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

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

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

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

my Activity implements the listener like that:

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

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

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.

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

回答by Janusz

The OnItemSelectedListenerlistens for list item selections and not list item clicks.

OnItemSelectedListener对列表项的选择,而不是列表项点击收听。

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

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

To get the wanted behavior one must use the OnItemClickListener.

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

回答by Edward Brey

It's because you happen to be testing with your fingers on a touch-enabled device. In touch mode, there is no focus and no selection. UIs that need selection should use a different type of widget, such as radio buttons.

这是因为您碰巧在支持触控的设备上用手指进行测试。在触摸模式下,没有焦点也没有选择。需要选择的 UI 应该使用不同类型的小部件,例如单选按钮。

回答by joe

At first,you should set ChoiceMode,and then,in ListView,there will not accept the selected event because setOnItemSelectedListenerregisted in AdapterView,and callback in method handleDataChanged(),but class AbsListViewoverride this method and never callback OnItemSelectedListener

首先,你应该设置ChoiceMode,然后,在ListView中,不会接受选择的事件,因为在方法中setOnItemSelectedListener注册AdapterView,并且在方法中回调handleDataChanged(),但是类AbsListView覆盖了这个方法并且永远不会回调OnItemSelectedListener

enter image description here

在此处输入图片说明

you can get the seletedItem by this method in setOnItemClickListener

你可以通过这个方法在 setOnItemClickListener

     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e("TAG", "onItemClick: " + position);
            SparseBooleanArray positions = mListView.getCheckedItemPositions();
            Log.e("TAG", "onItemSelected: " + positions.toString());

        }
    });