如何屏蔽ListView的Item中子控件优先获得焦点事件

    实际开发中,发现ListView中含有Button,ImageButtom,CheckBox等子控件时,ListView本身的Item无法获得单击事件,原因是,里面的子控件优先获得了单击事件。

    如何避免?这时候就可以使用descendantFocusability来解决。

    关于descendantFocusability属性,开发文档有详细描述:

    http://developer.android.com/reference/android/view/ViewGroup.html


    该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。

    属性的值有三种:

    

beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

    通常我们用到的是第三种,即在Item布局的根部加上android:descendantFocusability=”blocksDescendants”的属性就好了。

猜你喜欢

转载自essencer.iteye.com/blog/2062146