ListView已经不是简单的textview组成的item就能满足了,对于需要定制更多控件复杂界面的要求,什么ImageButton、Button、Edtitext等等之类对焦点会影响的控件,用在listview上就感觉有点酸爽~~~~
用了以后发现listview的item点击事件竟然失!效!了!
其实原因百度一下就知道了,无非就是因为ImageButton、Button、Edtitext这厮们霸占了焦点令到listview的点击事件失效(对不起语气重了,只是子view占用了父view,对于这种情况,有好也有不好,主要看各种码生的需求而已)。好,既然原因知道了,就可以去找解决方法了。
简单粗暴:
android:descendantFocusability="blocksDescendants"
把这个放到item的根布局上面去就好了。运行起来是不是感觉萌萌哒~~~~
官网解释:
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会覆盖子类控件而直接获得焦点