ListView防止点击事件被子view点击事件屏蔽

本文介绍如何解决ListView中Item点击事件被子View如Button抢夺焦点的问题,通过设置特定属性实现ListView ItemClick事件正常触发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ListView防止点击事件被子view点击事件屏蔽

在使用ListView的时候,我们使用adapter中画我们自定义item项时,一般会加上Button事件,或者Imagebutton,但是效果却不是我们想象的那么简单,因为你会发现ListView的itemclick事件就不生效了,原因很简单Button的onClick事件抢占了ListView的itemclick,我们只需在布置文件中添加下述代码屏蔽Button的onClick事件的焦点即可:

        在list的配置xml的根节点添加属性android:descendantFocusability="blocksDescendants",还有就是在要添加事件的控件上加android:focusable="false"。

我们看看

android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

ConstantValueDescriptionbeforeDescendants0The ViewGroup will get focus before any of its descendants.afterDescendants1The ViewGroup will get focus only if none of its descendants want it.blocksDescendants2The ViewGroup will block its descendants from receiving focus

最后一个是ViewGroup将阻止其后代接收焦点

所以 如果Listview中想不被屏蔽OnItemClickListener事件的话 那么久在item中的布局中 的最顶层布局加入这句话

想要阻止哪个widget 就在哪个widget设置focusable="false"

具体的实例看下面的

http://blog.chinaunix.net/space.php?uid=9935135&do=blog&id=181843

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值