ITEEDU

BaseExpandableListAdapter抽象类

public abstract class 
	BaseExpandableListAdapter
	extends Object
	implements ExpandableListAdapterHeterogeneousExpandableList

相当于BaseAdatper,只不过多了一套分组的List。

	private class ExpandableAdapter extends BaseExpandableListAdapter {				
		Activity activity;
		
		private List<GroupRecord> groupArray;		
		private List<List<UserRecord>> childArray;
		
		public ExpandableAdapter(Activity a) {
			activity = a;
		}

		public ExpandableAdapter(Activity a,List<GroupRecord> groupArray,List<List<UserRecord>> childArray) {
			activity = a;			
			this.groupArray = groupArray;
			this.childArray = childArray;
		}
		
		public Object getChild(int groupPosition, int childPosition) {
			if(childArray==null || childArray.size()==0){
				return null;
			}else{
				return childArray.get(groupPosition).get(childPosition);
			}			
		}

		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		public int getChildrenCount(int groupPosition) {
			if(childArray==null || childArray.size()==0){
				return 0;
			}else{
				return childArray.get(groupPosition).size();
			}			
		}
		
		class UserViewHolder {
			public ImageView profileImage;
			public TextView userText;
			......
		}
		
		
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			if(childArray.size()>groupPosition){
				List<UserRecord> uList = childArray.get(groupPosition);
				if(uList!=null && uList.size()>0){
					UserRecord user = uList.get(childPosition);
					UserViewHolder childVH;
					if(convertView==null){
						convertView = LayoutInflater.from(activity).inflate(
							R.layout.group_user, parent, false);
						childVH = new UserViewHolder();
						childVH.profileImage = (ImageView)convertView.findViewById(R.id.child_image);
						childVH.userText = (TextView)convertView.findViewById(R.id.child_name);
						...				
						convertView.setTag(childVH);
					}else{
						childVH = (UserViewHolder)convertView.getTag();
					}
					
					childVH.userText.setText(user.screenName);
					childVH.userPhone.setText(user.phone==null?"":user.phone);
					...
					convertView.setTag(childVH);				
					
					convertView.setOnClickListener(new OnClickListener(){
						@Override
						public void onClick(View v) {
							UserViewHolder userVHTemp = (UserViewHolder)v.getTag();
							Intent intent = ProfileActivity.createIntent(userVHTemp.user.id);
			                startActivity(intent);						
						}
					});
					
				}										
			}								
			return convertView;
		}

		// group method stub
		public Object getGroup(int groupPosition) {
			if(groupArray==null){
				return null;
			}else{
				return groupArray.get(groupPosition);
			}			
		}

		public int getGroupCount() {
			if(groupArray==null){
				return 0;
			}else{
				return groupArray.size();
			}			
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		
		class GroupViewHolder {			
			public TextView groupName;
			public ImageButton groupButton;	
			public int nGroupPosition;
			public boolean bGroupExpanded;			
		}
		
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			if(groupArray.size()>groupPosition){
				GroupRecord group = groupArray.get(groupPosition);			
				GroupViewHolder groupVH;
				if(convertView==null){
					convertView = LayoutInflater.from(activity).inflate(
							R.layout.group, parent, false);	
					groupVH = new GroupViewHolder();
					groupVH.groupName = ((TextView)convertView.findViewById(R.id.group_name));	
					groupVH.groupButton = ((ImageButton)convertView.findViewById(R.id.group_button));	

					convertView.setTag(groupVH);
				}else{
					groupVH = (GroupViewHolder)convertView.getTag();
				}
				
				groupVH.groupName.setText(group.name+" ["+group.relaNum+"]");
				groupVH.bGroupExpanded = isExpanded;
				groupVH.nGroupPosition = groupPosition;
				
				if(isExpanded){
					groupVH.groupButton.setVisibility(View.VISIBLE);
				}else{
					groupVH.groupButton.setVisibility(View.INVISIBLE);
				}
												
				groupVH.groupButton.setTag(menuDialog);
				......
				
				convertView.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						GroupViewHolder groupVHTemp = (GroupViewHolder)v.getTag();
						if(!groupVHTemp.bGroupExpanded){
							selGroupPosition = groupVHTemp.nGroupPosition;
							doRetrieve();
						}else{
							mGroupList.collapseGroup(groupVHTemp.nGroupPosition);
						}
					}
				});
			}
			return convertView;		
		}

		public boolean hasStableIds() {
			return false;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}				
	}