/** * Created by Administrator on 2018/10/19 0019. */ public class MyAdapter340 extends BaseExpandableListAdapter { Context context; List<Group> list; public MyAdapter340(Context context,List<Group> list){//构造函数,初始化MyAdapter340,设置入参数据 this.context=context; this.list=list; } //expandablelistview可以理解为List里面嵌套List,即把List的item设置为一个List //我将第一个定义为一级List,嵌套的称谓二级List @Override public int getGroupCount() {//返回二级List个数,用于设置一级List的item个数,也就是二级List个数 return list.size(); } @Override public int getChildrenCount(int groupPosition) {//返回二级List的子项个数,用于设置二级List的item个数 return list.get(groupPosition).getChilds().size(); } @Override public Object getGroup(int groupPosition) {//获取一级List的子项数据 return list.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) {//获取指定一级List的item(二级List)的指定item return list.get(groupPosition).getChilds().get(childPosition); } @Override public long getGroupId(int groupPosition) {//可扩展列表要求每个条目 (分组条目和子条目)具有一个可以唯一识别 // 列表中子条目和分组条目的ID. 该方法根据给定子条目ID和分组条目ID返回唯一识别ID.另外,如果 hasStableIds() // 为真,该函数返回的ID必须是固定不变的. return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { //groupPosition 决定返回哪个视图的组位置 . //isExpanded 该组是展开状态还是收起状态 . //convertView 如果可能,重用旧的视图对象.使用前你应该保证视图对象为非空,并且是否是合适的类型. // 如果该对象不能转换为可以正确显示数据的视图,该方法就创建新视图.不保证使用先前由 getGroupView(int, boolean,View, ViewGroup)创建的视图. //parent 该视图最终从属的父视图. if(convertView==null){ //如果convertView视图对象为空,则将xml文件解析生成一个View并依据partent的类型setView的LayoutParams(布局参数),但不加入到partent里面 convertView= LayoutInflater.from(context).inflate(R.layout.group_item340,parent,false); } //获取TextView组件 TextView group_name_textView = (TextView) convertView.findViewById(R.id.group_name_textView340); //获取一级list返回视图的对应位置(groupPosition)的Group对象 Group group = list.get(groupPosition); //设置一级List的显示文本 group_name_textView.setText(group.getGroup_name()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) { if(view==null){ //view,则将xml文件解析生成一个View并依据viewGroup的类型设置 View的LayoutParams(布局参数),但不加入到viewGroup里面 view=LayoutInflater.from(context).inflate(R.layout.child_item340,viewGroup,false); } //获取ImageView组件 ImageView child_photo_imageView = (ImageView) view.findViewById(R.id.child_photo_imageView340); //获取TextView组件 TextView child_name_textView = (TextView) view.findViewById(R.id.child_name_textView340); //获取二级List返回视图的对应位置坐标的item Group group = list.get(groupPosition); //获取二级List返回的item的对应位置 Child child = group.getChilds().get(childPosition); child_name_textView.setText(child.getChild_name()); child_photo_imageView.setImageResource(child.getPhoto()); return view; } @Override public boolean isChildSelectable(int i, int i1) { return false; } }
Android BaseExpandableListAdapter的用法解析
最新推荐文章于 2021-12-20 22:42:53 发布