当listview的item中有Button、ImageButton等自动获取焦点的控件时,可能点击每个item没有反应。这是因为子控件获取了焦点,则item获取不到焦点导致的。
我们可以通过descendantFocusability属性来解决这个问题:
这个属性大体是指:当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点(即ViewGroup具有优先权)
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点(即子类控件具有优先权,此属性值是默认的属性值)
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点(即 只有ViewGroup获取焦点)
如果我们想item获取的点击事件,只需要在Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的属性就好了;
代码示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="58dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:descendantFocusability="blocksDescendants" >