标签:
可以做出一个类似QQ分组的界面(点开分组,里面有人)Student类里面是name、age、sex的set get方法
Clazz类里面是clazznum、clazzname、List<Clazz>的setget方法public class MainActivity extends Activity {
private List<Clazz> mClazzs;
private ExpandableListView mExpandListview;
private ExpandableAdapter mAdapter;
private LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExpandListview = (ExpandableListView) findViewById(R.id.expandableListview);
gettData();
inflater=getLayoutInflater();
mAdapter=new ExpandableAdapter(mClazzs,inflater);
mExpandListview.setAdapter(mAdapter);
}private void gettData() {Expandableadapter继承自BaseExpandableListAdapter
mClazzs = new ArrayList<>();
List<Student> students = new ArrayList<>();
students.add(new Student("张三", "男", "18"));添加student的List
students.add(new Student("李四", "男", "19"));
students.add(new Student("王五", "男", "21"));
students.add(new Student("赵六", "男", "23"));
students.add(new Student("马奇", "男", "20"));
mClazzs.add(new Clazz("一班", students, "201501"));添加班级的List并把students这个List嵌套,构造器可以把students这个List传给Clazz
mClazzs.add(new Clazz("二班", students,"201502"));
mClazzs.add(new Clazz("三班", students,"201503"));
mClazzs.add(new Clazz("四班", students,"201504"));
}getGroupCount
getChildrenCount
getGroupView
getChildView
这四个复写的方法需要重写,其他的都是返回position@Override
public int getGroupCount() {
return mClazz.size();
}@Override
public int getChildrenCount(int groupPosition) {
return mClazz.get(groupPosition).getStudents().size(); 找到Clzz找到学生的ListView得到长度
}@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView=mInflater.inflate(R.layout.item_clazz,null);
TextView clazzName= (TextView) convertView.findViewById(R.id.textview_clazz_name);
TextView clazzNum= (TextView) convertView.findViewById(R.id.textview_clazz_num);
TextView clazzStuNum= (TextView) convertView.findViewById(R.id.textview_clazz_renshu);
Clazz clazz=mClazz.get(groupPosition);
clazzName.setText(clazz.getClazzName());
clazzNum.setText(clazz.getClazzNum());
clazzStuNum.setText(""+clazz.getStudents().size());//输出这种长度太小(不是本身带有的),计算机找不到,转化成String类型return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
convertView=mInflater.inflate(R.layout.item_student,null);
TextView studentName= (TextView) convertView.findViewById(R.id.textview_student_name);
TextView studentSex= (TextView) convertView.findViewById(R.id.textview_student_sex);
TextView studentAge= (TextView) convertView.findViewById(R.id.textview_student_age);
Clazz clazz=mClazz.get(groupPosition);得到对应的班级
List<Student> studentslist=clazz.getStudents();得到这个班级类中包含的student List
Student student=studentslist.get(childPosition);从List对应行得到学生对象
studentName.setText(student.getName());
studentAge.setText(student.getAge());
studentSex.setText(student.getSex());
return convertView;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
ExpandableListView(listView嵌套)
标签:
原文地址:http://blog.csdn.net/litao660044/article/details/48106499