android ExpandableListView 实现全选,全不选,部分选中,不可选择的功能

    最近一个项目要实现这样的功能:在ExpandableListView实现多选框,组选项的多选框要有四种状态:选中,没有选中,不可选,部份选中;子组项的多选框要有三种状态:选中,没有选中,不可选。首先想到使用是checkbox控件,但checkbox只有两种状态:选中,没有选中,怎么办好?重写checkbox控件吗?噢,no,,后来灵机一动:把checkbox换成图片不就行了。,现在把这部份抽出来,分享给大家:

先看效果图:

android ExpandableListView 实现全选,全不选,部分选中,不可选择的功能

这里没有做不可选状态,不过你看明白代码,你就知道怎么可以实现不可选的状态。

亮点;

1.我把组选项的,子选项的数据封装成对象。组选项的数据包含子选项的数据。

2.子选项的多选框状态控制组选项的状态变化。

3.很好复用了ListView,大家可以参考。

不多说:看代码:

组选项和子选项封装的代码:

public class Group {
	
	/** 标记是否选中的状态, 0为不可选,1为选中,2为没有选中,3为部份选中 */
	public int flag;
	/** 组选项所对应的组名 */
	public String groupname;
	/** 组选项下面子选项要显示的数据 */
	public ArrayList<Child> childData;
}
public class Child {
	
	/** 标记是否选中的状态, 0为不可选,1为选中,2为没有选中 */
	public int flag = 1;
	/** 子选项所对应的组名 */
	public String childname;

}

MainActivity类的代码:


public class MainActivity extends Activity {
	
	private ExpListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		
		ExpandableListView expandableListView = (ExpandableListView) this.findViewById(R.id.expandablelistview);
		
		ArrayList<Group> groupList = new ArrayList<Group>();
		ArrayList<Child> chlidList = null;
		Group group = null;
		Child child = null;
		for(int i = 0; i < 10; i++) {
			group = new Group();
			group.flag = 1;
			group.groupname = "组名" + i;
			
			chlidList = new ArrayList<Child>();
			for(int j = 0; j < 3; j++) {
				child = new Child();
				child.flag = 2;
				child.childname = "子名" + i + " " + j;
				chlidList.add(child);
			}
			
			group.childData = chlidList;
			
			groupList.add(group);
		}
		
		adapter = new ExpListAdapter(this, groupList, expandableListView);
		expandableListView.setAdapter(adapter);
		expandableListView.setOnChildClickListener(adapter);
		
		int groupCount = adapter.getGroupCount(); // 全部展开
		for (int i = 0; i < groupCount; i++) {
			expandableListView.expandGroup(i);
		}
	}

}
好了,到了最关键的代码:



ExpListAdapter类的代码,因为太长,只贴getChildView和getGroupView方法代码:


	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

		View view = convertView;
		ChildViewHolder childHolder = null; 
		if (null == view) {
			view = inflater.inflate(R.layout.child_item, null);
			childHolder = new ChildViewHolder();
			childHolder.upload_childcheckbox = (ImageView) view.findViewById(R.id.childcheckbox);
			childHolder.upload_childtextview = (TextView) view.findViewById(R.id.childtextview);
			view.setTag(childHolder);
		} else {
			childHolder = (ChildViewHolder) view.getTag();
		}
		
		if(childHolder.childOnClickListener == null) {
			childHolder.childOnClickListener = new ChildOnClickListener();
		}
		
		childHolder.childOnClickListener.setData(groupPosition, childPosition); // 传要点击的值过去
		childHolder.upload_childcheckbox.setOnClickListener(childHolder.childOnClickListener);
		
		String childname = dataArray.get(groupPosition).childData.get(childPosition).childname;
		childHolder.upload_childtextview.setText(childname);
		
		// 子选项的状态
		int i = 0;
		i = dataArray.get(groupPosition).childData.get(childPosition).flag;
		if(i == 1) {
			childHolder.upload_childcheckbox.setImageResource(R.drawable.check);
		} else if(i == 2) {
			childHolder.upload_childcheckbox.setImageResource(R.drawable.no_check);
		} else if(i == 0) {
			childHolder.upload_childcheckbox.setImageResource(R.drawable.uncheckable);
		}
		
		return view;
	}


	// 该方法决定每个组选项的外观
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

		View view = convertView;
		GroupViewHolder groupHolder = null;
		if (null == view) {
			view = inflater.inflate(R.layout.group_item, null);
			groupHolder = new GroupViewHolder();
			groupHolder.upload_groupcheckbox = (ImageView) view.findViewById(R.id.groupcheckbox);
			groupHolder.upload_groupmain = (TextView) view.findViewById(R.id.groupmain);
			groupHolder.upload_image = (ImageView) view.findViewById(R.id.image);
						
			view.setTag(groupHolder);
		} else {
			groupHolder = (GroupViewHolder) view.getTag();
		}
		
		if(groupHolder.groupOnClickListener == null) {
			groupHolder.groupOnClickListener = new GroupOnClickListener();
		}
		
		groupHolder.groupOnClickListener.setData(groupPosition); // 传要点击的值过去
		groupHolder.upload_groupcheckbox.setOnClickListener(groupHolder.groupOnClickListener);
		
		String s = dataArray.get(groupPosition).groupname;
		groupHolder.upload_groupmain.setText(s);
		
		int childsize = dataArray.get(groupPosition).childData.size();
		int flag = 0;
		int no_count = 0; // 不可选
		int select_count = 0; // 选中
		int count = 0; // 没有选中
		for(int j = 0; j < childsize; j++) {
			flag = dataArray.get(groupPosition).childData.get(j).flag;
			if(flag == 0) {
				no_count++ ;
			} else if(flag == 1) {
				select_count++;
			} else if(flag == 2) {
				count++;
			}
		}
		
		// 根据子选项的状态来判断组选项的多选框的状态
		if (no_count == childsize) { // 组选项多选项不可选
			dataArray.get(groupPosition).flag = 0;
		}
		if (select_count == childsize) { // 组选项多选项全选
			dataArray.get(groupPosition).flag = 1;
		}
		if (count == childsize) { // 组选项多选项没有选中
			dataArray.get(groupPosition).flag = 2;
		}
		if (select_count < childsize && select_count > 0) { // 组选项多选项部分选中
			dataArray.get(groupPosition).flag = 3;
		}
		
		int i = 0;
		
		// 组选项的状态
		i = dataArray.get(groupPosition).flag;
		if(i == 1) {
			groupHolder.upload_groupcheckbox.setImageResource(R.drawable.check);
		} else if(i == 2) {
			groupHolder.upload_groupcheckbox.setImageResource(R.drawable.no_check);
		} else if(i == 3) {
			groupHolder.upload_groupcheckbox.setImageResource(R.drawable.part_check);
		} else if(i == 0) {
			groupHolder.upload_groupcheckbox.setImageResource(R.drawable.uncheckable);
		}
		
		b = exList.isGroupExpanded(groupPosition); // 判断此组是否展开
		if (b) {
			groupHolder.upload_image.setBackgroundResource(R.drawable.group_icon_down);
		} else {
			groupHolder.upload_image.setBackgroundResource(R.drawable.group_icon_up);
		}
		
		return view;
	}
最后:贴上demo的代码。但我找不到在哪上传文件啊,求好心人帮忙。



你可能感兴趣的:(android,checkbox)