OnItemCLickListener 在列表视图中不起作用 [英] OnItemCLickListener not working in listview
问题描述
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);
}
}
在inflating时使用两个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屋!