<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="@+id/iv_home_page"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
<CheckBox
android:id="@+id/favorites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:checked="false"
android:background="@drawable/checkbox_customized_favorites" />
</FrameLayout>
</LinearLayout>
有时会碰到ListView的onItemClickListener事件无法响应,主要原因:listview的子元素中包含了Button或者ImageButton之类的元素,button的优先级高于listview 所以不能监听item的点击事件。
解决方法:在item 的根节点加入 “ android:descendantFocusability="blocksDescendants" 即可。
补充:
android:descendantFocusability的该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
android:descendantFocusability的属性值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
通常我们用到的是第三种,即在Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的属性就好了