码迷,mamicode.com
首页 > 其他好文 > 详细

ExpandableListView(listView嵌套)

时间:2015-08-30 19:44:16      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

可以做出一个类似QQ分组的界面(点开分组,里面有人)
在mainactivity中连接到界面,findById找到ExpandableListView,初始化数据,这个初始化是两个List的嵌套,把Student数据放到List<Student> students中,把Clazz数据放在List<Clazz> mClazz中,然后把students套进mClazz中(每个班级都有好多学生,每个mClazz中都有一个students的列表),嵌套是把生成的Clazz的对象(里面包含了student的List、班级名称、编号)放在了List中,然后把这个嵌套的List和初始化的Layoutinflater传给adapter,需要在layout中创建两个规则界面来分别配置GroupView和ChildView,把得来的数据解析,GroupView得到本行List(对应班级对象),里面包含班级名、编号和一个完整List,ChildView也解析这个数据把那个完整的List再解析得到每个学生的信息,然后各自添加到各自的Layout界面

Student类里面是name、age、sex的set get方法
Clazz类里面是clazznum、clazzname、List<Clazz>的setget方法

mainactivity

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() {
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"));
}
Expandableadapter继承自BaseExpandableListAdapter
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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!