活动类代码:

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


当前回答

对于我的列表,我的行有其他可以点击的东西,比如按钮,所以做一个毯子blocksDescendants是行不通的。相反,我在按钮的xml中添加了一行:

    android:focusable="false"

这样可以防止按钮阻塞对行的单击,但仍然允许按钮接受单击。

其他回答

我借助这个答案解决了它

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

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

 android:focusable="false" 

对于我的列表,我的行有其他可以点击的东西,比如按钮,所以做一个毯子blocksDescendants是行不通的。相反,我在按钮的xml中添加了一行:

    android:focusable="false"

这样可以防止按钮阻塞对行的单击,但仍然允许按钮接受单击。

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

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

在CustomListViewAdapter类上。

将其更改为:

return true;

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

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

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

android:descendantFocusability="blocksDescendants"

还不够。你必须设置

android:textIsSelectable="false"

到你的textviews和

android:focusable="false"

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