子控件无法获取焦点

在开发中,时常会碰到作为列表 Item 的复杂组合控件,这个时候,如果碰到 Item 无法获取 Focus,不能响应事件的情况(比如:ListView 的 Item 不能响应 onItenClick)时,有如下两种方法解决:

1、可以在 Item 的根布局上添加代码:
android:descendantFocusability="blocksDescendant"
2、如果不嫌麻烦的话,也可以通过单独给每个子控件设置 focusable 属性为 false 的方式解决。

总之,列表里面出现复杂的Item,导致列表事件或者Item事件不正常时,都可以尝试 方法1


ViewGroup 的源码片段:

/*** This view will get focus before any of its descendants.*/
public static final int FOCUS_BEFORE_DESCENDANTS = 0×20000;
/*** This view will get focus only if none of its descendants want it.*/
public static final int FOCUS_AFTER_DESCENDANTS = 0×40000;
/*** This view will block any of its descendants from getting focus, even if they are focusable.*/
public static final int FOCUS_BLOCK_DESCENDANTS = 0×60000;
/*** This view will get focus before any of its descendants.*/
public static final int FOCUS_BEFORE_DESCENDANTS = 0×20000;
/*** This view will get focus only if none of its descendants want it.*/
public static final int FOCUS_AFTER_DESCENDANTS = 0×40000;
/*** This view will block any of its descendants from getting focus, even if they are focusable.*/
public static final int FOCUS_BLOCK_DESCENDANTS = 0×60000;

你可能感兴趣的:(子控件无法获取焦点)