解决ListView item中含有Button或者Checkable的子类控件点击时冲突

由于在你自己定义的Item中存在诸如Button或者Checkable的子类控件,此时这些子控件会将焦点获取到,所以常常当点击item时变化的是子控件,item本身的点击没有响应。这时候就可以使用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.

该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:

  • beforeDescendants:viewgroup会优先其子类控件而获取到焦点
  • afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
  • blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

所以解决办法:

  1. 在Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的属性。
  2. 在当前ListView的xml里添加android:descendantFocusability=”blocksDescendants” 在item的xml里的Button添加android:focusable=”false”的属性。

猜你喜欢

转载自blog.csdn.net/qq_17853651/article/details/81202734