活动类代码:

conversationList = (ListView)findViewById(android.R.id.list);
ConversationArrayAdapter conversationArrayAdapter=new  ConversationArrayAdapter(this, R.layout.conversation_list_item_format_left, conversationDetails);
conversationList.setAdapter(conversationArrayAdapter);
conversationList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Log.d("test","clicked");
    }
});

Adapter类中的getView函数:

if (v == null) {                                
    LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(leftSideMessageNumber.equals(m.getTo())) {
        v = vi.inflate(R.layout.conversation_list_item_format_left, null);
    } else {
        v = vi.inflate(R.layout.conversation_list_item_format_right, null);
    }
}

在膨胀时使用两个xml会有问题吗?


当前回答

我也遇到了同样的问题,尝试了所有提到的解决方案都无济于事。通过测试,我发现使文本可选会阻止侦听器被调用。通过将它切换为false,或者删除它,我的监听器再次被调用。

android:textIsSelectable="false"

希望这能帮助像我这样被困住的人。

其他回答

问题是你的布局包含可聚焦或可点击的项目。 如果视图包含可聚焦或可点击的项,OnItemCLickListener将不会被调用。

点击这里了解更多信息。

如果不是这样的话,请发布一个你的布局xml。

我刚从这里找到了一个解决方案,不过是通过深度点击。

如果列表的任何行项包含可聚焦或可点击的视图,那么OnItemClickListener将不起作用。

行项必须具有类似的参数 android:descendantFocusability = "blocksDescendants"。

在这里您可以看到列表项应该是什么样的示例。 你的列表项xml应该是… row_item.xml (your_xml_file.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical" >

    // your other widgets here

</LinearLayout>

将此添加到主布局中 android: descendantFocusability = " blocksDescendants " 写这个代码到每个按钮,Textview,ImageView等 onClick android: focusable = " false " android:点击= " false "

希望它能起作用。

我借助这个答案解决了它

1.在“list_items.xml的线性布局”中添加如下内容 android: descendantFocusability = " blocksDescendants "

2.list_items.xml中线性布局的子视图

 android:focusable="false" 

在我的例子中,我必须从布局中删除下一行

android:clickable="true"