需求:
我有一个ListView,每个Items里的数据不是固定的(类似文件浏览器的应用),Item的布局中有一个CheckBox,我现在做一个CheckBox,当我这个CheckBox选中时,ListView中的每一个Item的CheckBox就全部选中
代码:
/** * 全选 */ private OnClickListener all_listener = new OnClickListener() { @Override public void onClick(View v) { fianl int count = listView.getChildCount(); for (int i = 0; i < count; i++) { final LinearLayout layout = (LinearLayout) listView.getChildAt(i); final int c = layout.getChildCount(); for (int j = 0; j < c; j++) { final View view = layout.getChildAt(j); if (view instanceof CheckBox) { ((CheckBox) view).setChecked(true); break; } } } } };