您的位置:首页 > 移动开发 > Android开发

Listview的onItemClickListener无法响应的解决方法

2013-08-31 15:32 519 查看
在android开发中,listview是我们应用最多的控件之一。listview不仅仅是用来显示数据,有时候在item中可能需要ImageButton,Button,CheckBox等子控件(也可以说是Button或者Checkable的子类控件),此时这些子控件会将焦点获取到,导致点击item无法响应。

这个时候就需要descendantFocusability来解决啦,descendantFocusability的API如下:

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.
ConstantValueDescription
beforeDescendants
0The ViewGroup will get focus before any of its descendants.
afterDescendants
1The ViewGroup will get focus only if none of its descendants want it.
blocksDescendants
2The ViewGroup will block its descendants from receiving focus.
This corresponds to the global attribute resource symbol 
descendantFocusability
.

Related Methods

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

属性的值有三种:

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

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

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

解决这一问题我们要用第三个值,即在Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的属性就好了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android listview OnItem
相关文章推荐