Android ListView嵌套checkbox点击事件


adapter添加:

public static Map isSelected;
private boolean defulat = false;

// 初始化
private void init() {
	// 这儿定义isSelected这个map是记录每个listitem的状态,初始状态全部为false。
	isSelected = new HashMap();
	for (int i = 0; i < mvList.size(); i++) {
		isSelected.put(i, defulat);
	}
}

holderView.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_view);
holderView.checkbox.setChecked(isSelected.get(position));

public static Map getIsSelected() {
		return isSelected;
	}

public static void setIsSelected(Map isSelected) {
		MainAdapter.isSelected = isSelected;
	}


activity的List点击事件:

private HolderView hv;

listContent.setOnItemClickListener(new OnItemClickListener() {
	@Override
	public void onItemClick(AdapterView arg0, View view, int position, long id) {
		hv = (HolderView) view.getTag();
		if (hv.checkbox.isChecked())
			hv.checkbox.setChecked(false);
		else
			hv.checkbox.setChecked(true);
		boolean check = hv.checkbox.isChecked();
		MainAdapter.isSelected.put(position, check);
		}
	});



你可能感兴趣的:(Android ListView嵌套checkbox点击事件)