ListViewの中にCheckBoxを入れるとsetOnItemClickListenerが動作しない サンプルコード有り
マニアックな話かもしれませんが、下記のスクショのように、リストビューの中に、チェックボックスを入れたい場合です。
普通にやると、チェックボックスは動作しますが、リストのタップしたところを判定して動作させるというのが動きません。

android:descendantFocusability="blocksDescendants"
を利用するとリストをタップできるようになります。
ここでは、inspections_list.xml内に利用しています。使うところがなんかわかりにくいですよね!
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder_inspections);
CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox);
try {
mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("item_name", "燃料");
map1.put("detail", "量・漏れ");
mylist.add(map1);
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("item_name", "エンジン");
map2.put("detail", "かかり具合・異音");
mylist.add(map2);
ListAdapter adapter = new SimpleAdapter(this, mylist,
R.layout.inspections_list, new String[] {
"item_name", "detail", "problem_comment"
},
new int[] {
R.id.item_name, R.id.detail, R.id.problem_comment
});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
//リストをクリックした時の処理
int item_id = position;
}
});
} catch (Exception e) {
Log.e(TAG, "Error parsing data " + e.toString());
}
}
inspections_list.xmlの内容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="7dp" android:descendantFocusability="blocksDescendants"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:orientation="horizontal" > <TextView android:id="@+id/item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/green1" android:textSize="18sp" /> </LinearLayout> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TableRow> <TextView android:id="@+id/detail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:padding="1dp" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="18sp" /> <CheckBox android:id="@+id/checkbox" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </TableRow> <TableRow> <TextView android:id="@+id/problem_comment" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_span="2" android:padding="1dp" android:text="@string/problem" android:textColor="@color/orange4" android:textSize="13sp" /> </TableRow> </TableLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="1dp" android:text="@string/problem" android:textColor="@color/orange4" android:textSize="13sp" /> </LinearLayout>
listplaceholder_inspections.xmlの内容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:divider="#cccccc" android:dividerHeight="1px" android:focusable="false" /> <TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/fetching" /> </LinearLayout>
