Android : ListView with Buttons -> OnItemClick do nothing

三世轮回 提交于 2019-12-04 10:36:38

问题


My problem come from ListView. Without Buttons, the .setOnItemClickListener of the ListView work well. (start an Activity)

At time that I add Buttons into items of the ListView, Buttons are clickable but the items aren't more clickable...

I try to use myListView.setItemCanFocus(true); but it don't work...


回答1:


Remove the focusable attribute from the Button would solve this problem. You could do that either in a layout xml file or java source code.

And one more tip, if you are using ImageButton instead of Button, you need setFocusable in your java code to make that work, cause the constructor of ImageButton would enabe this attribute after inflate from xml file.




回答2:


It might be better to use an onTouch() callback for the clickable button within the listview. You should then be able to click.on both the list item and the button. See this question for some code (no need for touchDelegate).




回答3:


The reason is button in your listview absorbs the onItemClickEvent.

A well explained tutorial is here




回答4:


You can use this in .setOnItemClickListener of the ListView

view.findViewById(R.id.btn_id).setOnClickListener(new View.OnClickListener(){//your method})


来源:https://stackoverflow.com/questions/6275183/android-listview-with-buttons-onitemclick-do-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!