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

来源:互联网 发布:lumia2520 linux 编辑:程序博客网 时间:2024/04/28 18:04

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