android listView设置了点击事件 点击无效

来源:互联网 发布:用u盘重装mac 编辑:程序博客网 时间:2024/06/07 00:34

一.问题引入

ListView控件:给Item设置了点击事件,却没有效果。

二.解决方案

ListView使用了自定义布局文件,在布局文件中有button等控件时,这些控件获取焦点的级别比listView的item高,所以当点击item时,button等控件会优先获得点击焦点。

解决方法就是在布局文件根元素中添加属性: Android:descendantFocusability="blocksDescendants"

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.

 

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

属性的值有三种:

        beforeDescendants:viewgroup会优先其子类控件而获取到焦点

        afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

        blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点


0 0
原创粉丝点击