Android---自定义带CheckBox的ExpandableListView实现

   上篇文章讲了自定义带CheckBox的LISTVIEW的实现,这篇来讲讲自定义带CheckBox的ExpandableListView实现,原理思想跟LISTVIEW是一样的,只是数据存入读取方面做  相应的修改即可。

    同样的,先创建一个工程,然后修改main.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" >
    
    <Button
        android:id="@+id/select_btn"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_gravity="center_horizontal"
    	android:text = "SelectAll"
    />
 	<!-- 
 	  android:listSelector="@android:color/transparent"可以去掉点击时默认的黄色背景
 	  android:divider="@android:color/transparent"可以去掉默认的分割线
 	  这两个设置了后就可以换成自己的背景了。
 	-->
	    <ExpandableListView 
			android:id="@+id/expandableListView"
		    android:layout_width="match_parent" 
		    android:layout_height="wrap_content"
		    android:clickable="true" 
	    />

</LinearLayout>

     然后需要写GROUP的布局文件,我的GROUP布局文件有一个TextView和一个ImageView组成,如listview_group_item.xml

 

<?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="horizontal" >  
	<TextView 
        android:id="@+id/group_name"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="5dip"  
		android:textSize="16dip"
     />
    <RelativeLayout 
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
        >
    
	<ImageView 
	    android:id="@+id/icon"
	    android:layout_width="30dp"
	    android:layout_height="27dp"
		android:layout_marginRight="5dip"  
		android:layout_alignParentRight="true"
		android:gravity="center_vertical" 
	/>
	</RelativeLayout> 
</LinearLayout>
    

      然后还需要一个CHILD的布局文件,我是由一个TextView和一个CheckBox组成的。如listview_item.xml

<?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="horizontal" >  
	<TextView 
        android:id="@+id/name"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="5dip"  
		android:textSize="12dip"
     />
    <RelativeLayout 
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
        >
    
	<CheckBox 
	    android:id="@+id/listcb"
	    android:layout_width="30dp"
	    android:layout_height="27dp"
		android:layout_marginRight="5dip"  
		android:layout_alignParentRight="true"
		android:gravity="center_vertical"
		android:focusable="false"
		android:clickable="false"	
		android:focusableInTouchMode="false" 
		android:button="@android:color/transparent"
		android:background="@drawable/music_listview_checkbox"	
	/>
	</RelativeLayout> 
</LinearLayout>

        紧接着写ExpandableListViewAdapter.java文件:

package com.wm.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
	private List<String>		mGroupData = new ArrayList<String>();
	private List<HashMap<Integer,String>> mChildData= new ArrayList<HashMap<Integer,String>>(); 
//    private Context 				mContext;
    LayoutInflater 					mInflater;
    
	// 用来控制CheckBox的选中状况  
	private  List<HashMap<Integer,Boolean>> isSelected;  
    
	public ExpandableListViewAdapter(LayoutInflater inflater,Context mcontext,List<String> mGroup,List<HashMap<Integer,String>> mChild) 
	{
		mInflater = inflater;
	//	mContext = mcontext;
		isSelected = new ArrayList<HashMap<Integer,Boolean>>();
		mGroupData = mGroup;
		mChildData = mChild;
		initSelectedMap();
	}  
	
	public void initSelectedMap(){
		//默认全部未勾选状态。
		for(int groupId = 0; groupId < mGroupData.size();++groupId){
			HashMap<Integer,Boolean> mChildCheck = new HashMap<Integer,Boolean>();
			for(int childId = 0; childId< mChildData.get(groupId).size();++childId){
				mChildCheck.put(childId, false);
			}	
			isSelected.add(mChildCheck);
		}
	}

	public void setListViewData(List<String> mGroup,List<HashMap<Integer,String>> mChild){
		mGroupData = mGroup;
		mChildData = mChild;
	}
	
	public void setItemCheckBoxStatus(View mView,int groupPosition,int childPostion){

		ViewTag holder = (ViewTag) mView.getTag();  
		// 改变CheckBox的状态  
		holder.mCheckBox.toggle();  
		// 将CheckBox的选中状况记录下来  
		isSelected.get(groupPosition).put(childPostion, holder.mCheckBox.isChecked()); 

	}
	
	public void setAllCheckBoxStatus(Boolean mFlag){
		for(int groupId = 0; groupId < mGroupData.size();++groupId){
			for(int childId = 0; childId< mChildData.get(groupId).size();++childId){
				isSelected.get(groupId).put(childId, mFlag);
			}		
		}

	}

	public class ViewTag{
		public TextView mTextView;
		public CheckBox mCheckBox;
		public ImageView mIcon;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mChildData.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewTag mVobj = new ViewTag();
		View v;
		
		if(convertView != null){
			v = convertView;
		}
		else{
			v = mInflater.inflate(R.layout.listview_item, null);	
		}
		mVobj.mTextView	= (TextView) v.findViewById(R.id.name);
	    mVobj.mCheckBox = (CheckBox) v.findViewById(R.id.listcb);
	    mVobj.mCheckBox.setChecked(isSelected.get(groupPosition).get(childPosition));
	    mVobj.mTextView.setText(mChildData.get(groupPosition).get(childPosition));
	    v.setTag(mVobj);
		return v;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return mChildData.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mGroupData.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mGroupData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewTag mVobj = new ViewTag();
		View v;
		
		if(convertView != null){
			v = convertView;
		}
		else{
			v = mInflater.inflate(R.layout.listview_group_item, null);	
		}
		mVobj.mTextView	= (TextView) v.findViewById(R.id.group_name);
	    mVobj.mTextView.setText(mGroupData.get(groupPosition));
	    mVobj.mIcon = (ImageView) v.findViewById(R.id.icon);
	    
	    v.setTag(mVobj);
	    
	     if (isExpanded) { 
             mVobj.mIcon.setImageResource(R.drawable.expandlist_up); 
	     } else { 
	    	 mVobj.mIcon.setImageResource(R.drawable.expandlist_down); 
	     } 
		return v;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
}

    最后完成该ACTIVITY(ExpandableListViewCheckbox.java)的操作控制,我这里多加了一个allSelected/allUnSelected按钮功能:

package com.wm.example;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

public class ExpandableListViewCheckbox extends Activity implements OnClickListener{
	
	private static final String TAG="ExpandableListViewCheckboxActivity";
	private Button 				mBtn;
	private ExpandableListView			mListView;
	private ExpandableListViewAdapter 	mListViewAdapter = null;
	
	private List<String>		mGroupData = new ArrayList<String>();
	private List<HashMap<Integer,String>> mChildData= new ArrayList<HashMap<Integer,String>>(); 
	//control select button status(SelectedAll or UnselectAll)
	private	Boolean				mAllSelected = false;
	
	private List<HashMap<Integer,Boolean>> 	mCheckedObj = new ArrayList<HashMap<Integer,Boolean>>();
	private int mAllSelectedSize = 0;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mBtn=(Button) findViewById(R.id.select_btn);
        mBtn.setOnClickListener(this);
        
		mListView = (ExpandableListView) findViewById(R.id.expandableListView);
		mListView.setGroupIndicator(null);
		InitData();
		mListView.setOnGroupClickListener(new OnGroupClickListener(){

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub
				
				return false;
			}
			
		});
		
		mListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				//Log.w(TAG,"********onChildClick*********");
				mListViewAdapter.setItemCheckBoxStatus(v, groupPosition,childPosition);
				if(mCheckedObj.get(groupPosition).containsKey(childPosition)){
				//	Log.w(TAG,"****onItemClick***NEED REMOVE***");
					mCheckedObj.get(groupPosition).remove(childPosition);
				}
				else{
				//	Log.w(TAG,"****onItemClick***NEED put***");
					mCheckedObj.get(groupPosition).put(childPosition, true);		
				}
				
				if(getCheckedObjSize() == 0){
				//	Log.w(TAG,"****onItemClick**NO CHECKED***");
					mAllSelected = false;
					mBtn.setText("SelectAll");
				}
				else if(getCheckedObjSize() == mAllSelectedSize){
					mAllSelected = true;
					mBtn.setText("UnselectAll");		
				}
				return false;
			}
			
		});


		mListViewAdapter = new ExpandableListViewAdapter(getLayoutInflater(),this,mGroupData,mChildData);
		mListView.setAdapter(mListViewAdapter);
		
    }
    
    private HashMap<Integer,String> setHashMap(int size,String mstring){
    	HashMap<Integer,String> temp = new HashMap<Integer,String>();
    	for(int i=0;i< size; ++i){
    		temp.put(i, mstring+i);
    	}  	
    	return temp;
    }
    
    private void InitData(){
    	mGroupData.add("aaa");
    	mChildData.add(setHashMap(2,"a"));
    	
    	mGroupData.add("bbb");
    	mChildData.add(setHashMap(4,"b"));
    	
    	mGroupData.add("ccc");
    	mChildData.add(setHashMap(3,"c"));
    	
    	mGroupData.add("ddd");
     	mChildData.add(setHashMap(1,"d"));
     	
    	mGroupData.add("eee");
     	mChildData.add(setHashMap(2,"e"));
     	
    	mGroupData.add("fff");
     	mChildData.add(setHashMap(5,"f"));
     	
    	mGroupData.add("ggg");
    	mChildData.add(setHashMap(1,"g"));
    	for(int groupId = 0; groupId < mGroupData.size();++groupId){
    		HashMap<Integer,Boolean> temp = new HashMap<Integer,Boolean>();
    		for(int childId = 0; childId < mChildData.get(groupId).size();++childId){
    			mAllSelectedSize++;
    		}
    		mCheckedObj.add(temp);
    	}
    	
    }

    private int getCheckedObjSize(){
    	int size =0;
    	for(int groupId = 0; groupId < mGroupData.size();++groupId){
    		size += mCheckedObj.get(groupId).size();
    	}
    	return size;
    }
    
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
			case R.id.select_btn:{
				if(mAllSelected){					
					mAllSelected = false;
					mBtn.setText("SelectAll");
					mListViewAdapter.setAllCheckBoxStatus(false);
			    	for(int groupId = 0; groupId < mGroupData.size();++groupId){
			    		mCheckedObj.get(groupId).clear();
			    	}
				}
				else{
					mAllSelected = true;
					mBtn.setText("UnselectAll");
					mListViewAdapter.setAllCheckBoxStatus(true);
			    	for(int groupId = 0; groupId < mGroupData.size();++groupId){
			    		for(int childId = 0; childId < mChildData.get(groupId).size();++childId){
			    			mCheckedObj.get(groupId).put(childId, true);
			    		}
			    	}
				}
				//Refresh data
				mListViewAdapter.notifyDataSetChanged();
			}
			break;
			default:break;
		}
		
	}
}

      最后运行即可测试效果。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值