Android : ListView with Buttons -> OnItemClick do

2019-03-18 18:49发布

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...

4条回答
闹够了就滚
2楼-- · 2019-03-18 18:55

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楼-- · 2019-03-18 19:00

The reason is button in your listview absorbs the onItemClickEvent.

A well explained tutorial is here

查看更多
混吃等死
4楼-- · 2019-03-18 19:01

You can use this in .setOnItemClickListener of the ListView

view.findViewById(R.id.btn_id).setOnClickListener(new View.OnClickListener(){//your method})
查看更多
Evening l夕情丶
5楼-- · 2019-03-18 19:05

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.

查看更多
登录 后发表回答