- 直接看代码吧,也是自己mark下
List<CombinModel> parentList;
List<List<CombinModel>> childList;
Context context;
LayoutInflater inflater;
public CombindAdapte(List<CombinModel> parentList, List<List<CombinModel>> childList, Context context) {
this.parentList = parentList;
this.childList = childList;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public int getGroupCount() {
return parentList.size();
}
@Override
public Object getGroup(int groupPosition) {
return parentList.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_stage_list, null);
holder = new ViewHolder();
holder.checkBox = (CheckBox) convertView.findViewById(R.id.id_treenode_write);
holder.tvName = (TextView) convertView.findViewById(R.id.id_treenode_label);
holder.tvRate = (TextView) convertView.findViewById(R.id.answer_num);
holder.rootLine = convertView.findViewById(R.id.root_line);
holder.cirBotmLine = convertView.findViewById(R.id.line);
holder.icon = (ImageView) convertView.findViewById(R.id.id_treenode_icon);
holder.cirTopLine = convertView.findViewById(R.id.top_line);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final CombinModel node = parentList.get(groupPosition);
holder.tvName.setText(node.getKlName());
holder.tvRate.setText(node.getRightRate());
if (childList.get(groupPosition) != null && childList.get(groupPosition).size() > 0) {
//如果当前分组的下面有child
if (!isExpanded) {
//gruop有孩子并且如果没有打开
holder.cirTopLine.setVisibility(View.INVISIBLE);
holder.cirBotmLine.setVisibility(View.GONE);
holder.icon.setImageResource(R.drawable.tree_indicator1_fold);
holder.rootLine.setVisibility(View.VISIBLE);
} else {
//grup有child 并且展开
holder.cirBotmLine.setVisibility(View.VISIBLE);
holder.rootLine.setVisibility(View.GONE);
holder.icon.setImageResource(R.drawable.tree_indicator1_expand);
}
} else {
//gruop没有child
holder.cirTopLine.setVisibility(View.INVISIBLE);
holder.cirBotmLine.setVisibility(View.GONE);
holder.icon.setImageResource(R.drawable.tree_indicator1_none);
holder.rootLine.setVisibility(View.VISIBLE);
}
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
node.setCheck(true);
} else {
node.setCheck(false);
}
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_stage_list, null);
holder = new ViewHolder();
holder.checkBox = (CheckBox) convertView.findViewById(R.id.id_treenode_write);
holder.tvName = (TextView) convertView.findViewById(R.id.id_treenode_label);
holder.tvRate = (TextView) convertView.findViewById(R.id.answer_num);
holder.rootLine = convertView.findViewById(R.id.root_line);
holder.cirBotmLine = convertView.findViewById(R.id.line);
holder.icon = (ImageView) convertView.findViewById(R.id.id_treenode_icon);
holder.cirTopLine = convertView.findViewById(R.id.top_line);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final CombinModel node = childList.get(groupPosition).get(childPosition);
//这里做判断如何显示
if (childPosition == childList.get(groupPosition).size() - 1) {
//如果是最后一个子chidl
holder.rootLine.setVisibility(View.VISIBLE);
holder.cirBotmLine.setVisibility(View.GONE);
} else {
//不是最后一个项目
holder.cirBotmLine.setVisibility(View.VISIBLE);
holder.rootLine.setVisibility(View.GONE);
}
holder.cirTopLine.setVisibility(View.VISIBLE);
holder.icon.setImageResource(R.drawable.tree_indicator1_none);
holder.tvName.setText(node.getKlName());
holder.tvRate.setText(node.getRightRate());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
node.setCheck(true);
} else {
node.setCheck(false);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class ViewHolder {
TextView tvName;
TextView tvRate;
CheckBox checkBox;
View rootLine;//父节点下面的灰色线
View cirTopLine;//圆上面的线
View cirBotmLine;//圆下面的线
ImageView icon;//加好或者减号 或者空心圆
}
- 接下来就是数据的封装了
private void getAllDKLdata(JSONArray array) {
parentList = new ArrayList<>();
chidlList = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
JSONObject json = array.getJSONObject(i);
CombinModel model = new CombinModel();
model.setRightRate(json.getString("rightRate"));
model.setKlID(json.getString("klID"));
model.setKlName(json.getString("klName"));
parentList.add(model);
if (json.getString("sub") != null && json.getJSONArray("sub").size() > 0) {
List<CombinModel> temp = new ArrayList<>();
JSONArray subArray = json.getJSONArray("sub");
for (int j = 0; j < subArray.size(); j++) {
CombinModel model2 = new CombinModel();
JSONObject subJson = subArray.getJSONObject(j);
model2.setKlName(subJson.getString("klName"));
model2.setKlID(subJson.getString("klID"));
model2.setRightRate(subJson.getString("rightRate"));
temp.add(model2);
}
chidlList.add(temp);
} else {//这里需要注意,哪怕里面没有数据,这里也得实例化个空的。不然会出现顺序问题。么么哒
chidlList.add(new ArrayList<CombinModel>());
}
}
}
接下来是 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
<include
android:id="@+id/step"
layout="@layout/item_step" />
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:childDivider="@null"
android:divider="@null" />
</LinearLayout>
记得在 代码中添加
expandableListView.setGroupIndicator(null);
代码。把默认指针去掉
响应条目点击事件
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
//child的条目点击事件
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
return true;
}
});
这里还需要注意adapter中也需要修改
覆写BaseExpandableListAdapter的isChildSelectable()的返回值为true;
调用ExpandableListView对象的setOnChildClickListener()方法,为其绑定监听器