在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.
Constant | Value | Description |
---|---|---|
beforeDescendants | 0 | The ViewGroup will get focus before any of its descendants. |
afterDescendants | 1 | The ViewGroup will get focus only if none of its descendants want it. |
blocksDescendants | 2 | The 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”的属性就好了。