如题,在网上查了很多,大体上都会说焦点获取的问题,但自己使用时发现并没有解决我的问题,经过一点调试,发现问题的具体原因及解决办法。
首先,大部分的解决方案:
如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我们可以通过将ListView中Item中包含的所有控件的focusable属性设置为false,这样的话ListView的Item自动获得了Focus的权限,也就可以被选中了。再就是关于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会覆盖子类控件而直接获得焦点
上面说的是原理,按照上面的还是没有解决的,具体要强调一点:
android:descendantFocusability="blocksDescendants"这句,不是加在ListView中,而是加在ListView的ItemLayout的根布局上。