1.以下为ExpandableListview的适配器,在我从网上找了很多之后,发现还是这个好用,并且最终在项目中用了这个(这个是放在Activity中的,没有单独的Adapter)。
/**
* ExpandableListview的适配器
*/
class MyAdapter extends BaseExpandableListAdapter {
Context mcontext;
public MyAdapter(Context context) {
mcontext = context;
}
//得到子item需要关联的数据
@Override
public Object getChild(int groupPosition, int childPosition) {
String key = parents.get(groupPosition);
return (map.get(key).get(childPosition));
}
//得到子item的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//设置子item的组件
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String key = ChoosePeiJianForExpireActivity.this.parents.get(groupPosition);
String info = map.get(key).get(childPosition);
ChildViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mcontext)
.inflate(R.layout.item_tree_child, null);
holder = new ChildViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ChildViewHolder) convertView.getTag();
}
holder.tv_child.setText(info);
return convertView;
}
//获取当前父item下的子item的个数
@Override
public int getChildrenCount(int groupPosition) {
String key = parents.get(groupPosition);
int size=map.get(key).size();
return size;
}
//获取当前父item的数据
@Override
public Object getGroup(int groupPosition) {
return parents.get(groupPosition);
}
@Override
public int getGroupCount() {
return parents.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//设置父item组件
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mcontext)
.inflate(R.layout.item_tree_group, null);
holder = new GroupViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (GroupViewHolder) convertView.getTag();
}
holder.tv_group.setText(ChoosePeiJianForExpireActivity.this.parents.get(groupPosition));
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/*绑定视图中的控件*/
class GroupViewHolder {
TextView tv_group;
ImageView iv_group_on,iv_group_down;
GroupViewHolder(View view) {
tv_group = (TextView) view.findViewById(R.id.tv_group);
iv_group_on = (ImageView) view.findViewById(R.id.iv_group_on);
iv_group_down = (ImageView) view.findViewById(R.id.iv_group_down);
}
}
class ChildViewHolder {
TextView tv_child;
ImageView iv_child_choosenull,iv_child_chooseyes;
ChildViewHolder(View view) {
tv_child = (TextView) view.findViewById(R.id.tv_child);
iv_child_choosenull = (ImageView) view.findViewById(R.id.iv_child_choosenull);
iv_child_chooseyes = (ImageView) view.findViewById(R.id.iv_child_chooseyes);
}
}
}
2.在getChildView和getGroupView方法中找到自己自定义的布局,子布局和父布局必须进行视图绑定,并且在ViewHolder中得到自己需要的控件进行相应的处理。
如果没有以下步骤,就不会得到自己自定义的布局,而且父布局和子布局不能放在一起,要分开放。(惭愧啊,踩过这个坑)。
class GroupViewHolder {
TextView tv_group;
ImageView iv_group_on,iv_group_down;
GroupViewHolder(View view) {
tv_group = (TextView) view.findViewById(R.id.tv_group);
iv_group_on = (ImageView) view.findViewById(R.id.iv_group_on);
iv_group_down = (ImageView) view.findViewById(R.id.iv_group_down);
}
}
class ChildViewHolder {
TextView tv_child;
ImageView iv_child_choosenull,iv_child_chooseyes;
ChildViewHolder(View view) {
tv_child = (TextView) view.findViewById(R.id.tv_child);
iv_child_choosenull = (ImageView) view.findViewById(R.id.iv_child_choosenull);
iv_child_chooseyes = (ImageView) view.findViewById(R.id.iv_child_chooseyes);
}
}