OnItemCLickListener在列表视图中不起作用 [英] OnItemCLickListener not working in listview

查看:67
本文介绍了OnItemCLickListener在列表视图中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Activity类代码:

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是否存在问题?

Is there a problem with using two xmls while inflating?

推荐答案

我只是从这里找到了一个解决方案,但是要进行深度点击.

I just found a solution from here, but by deep clicking.

如果列表中的任何行项目包含可聚焦或可单击的视图,则OnItemClickListener将不起作用.

If any row item of list contains focusable or clickable view then OnItemClickListener won't work.

该行项目必须具有以下参数 android:descendantFocusability = "blocksDescendants".

The row item must have a param like android:descendantFocusability = "blocksDescendants".

在这里,您可以看到有关列表项外观的示例. 您的清单项目xml应该是... row_item.xml(your_xml_file.xml)

Here you can see an example of how your list item should look like. Your list item xml should be... 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>

这篇关于OnItemCLickListener在列表视图中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆