item点击无效大多数情况是因为item布局中含有ImageButton,Button,CheckBox等子控件。在点击item时,这些子控件会首先获取焦点,所以点击item时变化的是子控件,item本身的点击无效。
以前处理这类问题都是使用
android:focusable=""
android:focusableInTouchMode=""
来处理,最近新发现了一个新的属性可以更好的处理这类问题,就是
android: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.
粗略翻译如下:
定义ViewGroup及其子控件在View中获取焦点时的关系。
属性的值有三种:
beforeDescendants: viewgroup优先其子类控件而获取到焦点
afterDescendants: viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants: viewgroup覆盖子类控件而直接获得焦点
以上三个常量值根据需求选择一项,我使用的是blocksDescendants