AI智能
改变未来

android ExpandableListView在项目中的使用


android ExpandableListView在项目中的使用

最近在项目中涉及到树形菜单结构数据的管理,数据展现使用安卓原生ExpandableListView,本质上和listview没有多大差别,lisview的数据渲染和布局复用只需重写baseadapter的getview()方法,而ExpandableListView的自定义adpater主要是对BaseExpandableListAdapter中的getGroupView和getChildView进行数据渲染、处理,下面贴上部分代码。

public class AreaExpandAdapter extends BaseExpandableListAdapter {private Context mContext;private ArrayList<AreaModel> areaModels;public AreaExpandAdapter(Context mContext, ArrayList<AreaModel> areaModels) {this.mContext = mContext;this.areaModels = areaModels;}@Overridepublic int getGroupCount() {return areaModels != null ? areaModels.size() : 0;}@Overridepublic int getChildrenCount(int groupPosition) {return areaModels.get(groupPosition).equipments != null ? areaModels.get(groupPosition).equipments.size() : 0;}@Overridepublic Object getGroup(int groupPosition) {return areaModels.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {return areaModels.get(groupPosition).equipments.get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {GroupHolder holder ;if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.item_parent, null);holder = new GroupHolder(convertView);convertView.setTag(holder);} else {holder = (GroupHolder) convertView.getTag();}if (isExpanded){holder.img_expand.setImageDrawable(mContext.getDrawable(R.drawable.ico_up));}else {holder.img_expand.setImageDrawable(mContext.getDrawable(R.drawable.ico_down));}holder.cbx_parent.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {List<Equipment> equipments=areaModels.get(groupPosition).equipments;if (areaModels.get(groupPosition).ischecked){areaModels.get(groupPosition).ischecked=false;for (Equipment equipment:equipments){equipment.ischecked=false;EventBusUtil.sendEvent(new MessageEvent(EventCode.del_RFID,equipment.LockRFID));}}else {areaModels.get(groupPosition).ischecked=true;for (Equipment equipment:equipments){equipment.ischecked=true;EventBusUtil.sendEvent(new MessageEvent(EventCode.add_RFID,equipment.LockRFID));}}notifyDataSetChanged();}});holder.update(areaModels.get(groupPosition));return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {ChildHolder childViewHolder;if (convertView==null){convertView = LayoutInflater.from(mContext).inflate(R.layout.item_child,null);childViewHolder = new ChildHolder(convertView);convertView.setTag(childViewHolder);}else {childViewHolder = (ChildHolder) convertView.getTag();}childViewHolder.update(areaModels.get(groupPosition).equipments.get(childPosition));childViewHolder.cbx_child.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){EventBusUtil.sendEvent(new MessageEvent(EventCode.add_RFID,areaModels.get(groupPosition).equipments.get(childPosition).LockRFID));}else {EventBusUtil.sendEvent(new MessageEvent(EventCode.del_RFID,areaModels.get(groupPosition).equipments.get(childPosition).LockRFID));}}});return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}class GroupHolder {private TextView tv_area_name;private ImageView img_expand,cbx_parent;public GroupHolder(View v) {tv_area_name = (TextView) v.findViewById(R.id.tv_area_name);img_expand = (ImageView) v.findViewById(R.id.img_expand);cbx_parent = (ImageView) v.findViewById(R.id.cbx_parent);}public void update(AreaModel model) {tv_area_name.setText(model.araeName);if (model.ischecked){cbx_parent.setImageDrawable(mContext.getDrawable(R.drawable.ico_rg));}else {cbx_parent.setImageDrawable(mContext.getDrawable(R.drawable.ico_rg1));}}}class ChildHolder {private TextView tv_equipment_name,tv_equipment_kv;private CheckBox cbx_child;public ChildHolder(View v) {tv_equipment_name = (TextView) v.findViewById(R.id.tv_equipment_name);tv_equipment_kv = (TextView) v.findViewById(R.id.tv_equipment_kv);cbx_child = (CheckBox) v.findViewById(R.id.cbx_child);}public void update(Equipment model) {tv_equipment_name.setText(model.equipmentName);tv_equipment_kv.setText(model.VoltageLevelName);cbx_child.setChecked(model.ischecked);}}}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » android ExpandableListView在项目中的使用