活动类代码:

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


当前回答

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

android:clickable="true"

其他回答

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

    android:focusable="false"

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

我也有同样的问题,我在行布局中使用了具有“可聚焦”属性的文本样式。我把它取下来后它还能用。

在列表视图的自定义行布局中使用下面的代码

 android:focusable="false"
 android:clickable="false"

使用android: descendantFocusability

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dip"
    android:background="@color/light_green"
    android:descendantFocusability="blocksDescendants" >

在根布局中添加上述内容

即使我有同样的问题,我有复选框,做下面的掩码itemClickListener工作,

在复选框中添加以下属性,

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

ItemClickListner开始工作。

详细的例子你可以点击链接,

http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

希望能有所帮助,干杯!!