活动类代码:

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会有问题吗?


当前回答

我也遇到了同样的问题,我只是看到我不小心设置了:

@Override
public boolean isEnabled(int position)
{
    return false;
}

在CustomListViewAdapter类上。

将其更改为:

return true;

我已经设法解决了这个问题。 以防万一有人犯了同样的错误……

其他回答

以上这些对我来说都失败了。然而,我能够解决这个问题(经过几个小时的敲击我的头-谷歌,如果你在听,请考虑修复我在编译器错误的形式,如果可能的话)

你真的需要小心你添加到xml布局的android属性(在这个原始问题中,它被称为list_items.xml)。对我来说,导致问题的原因是我已经从EditText视图切换到TextView,并且从更改中有剩余的属性cruft(在我的情况下,inputType)。编译器没有捕捉到它,当我去运行应用程序时,可点击性刚刚失败。再次检查你在布局xml节点中拥有的所有属性。

如果你有文本视图,按钮或STG可点击或选择在你的行视图

android:descendantFocusability="blocksDescendants"

还不够。你必须设置

android:textIsSelectable="false"

到你的textviews和

android:focusable="false"

到您的按钮和其他可对焦的项目。

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

android:clickable="true"

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

如果列表的任何行项包含可聚焦或可点击的视图,那么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>

如果你想同时使用简单点击和长时间点击列表视图项,更好的实现方式是使用上下文菜单长时间点击。避免使用setItemLongClickListener,特别是当你的列表视图有多行布局时。