android自定义expandlistview,自定义ExpandableListView

可扩展的listview,类似于好友分组

已班级和学生为例

一,创建ExpandaListView布局,item_class,item_student布局

主布局

item_clazz

item_student

二,创建moudel clazz和student

clazz:

package com.lingzhuo.myexpandlistview.moudel;

import java.util.List;

/** * Created by heinika on 2015/8/26. */

public class Clazz {

private List students;

private String clazzName;

private String id;

public Clazz(String clazzName, String id) {

this.clazzName = clazzName;

this.id = id;

}

public List getStudents() {

return students;

}

public void setStudents(List students) {

this.students = students;

}

public String getClazzName() {

return clazzName;

}

public void setClazzName(String clazzName) {

this.clazzName = clazzName;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

}

student:

package com.lingzhuo.myexpandlistview.moudel;

/** * Created by heinika on 2015/8/26. */

public class Student {

private String name;

private String age;

private String sex;

private String lable;

public Student(String name, String age, String sex, String lable) {

this.name = name;

this.age = age;

this.sex = sex;

this.lable = lable;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAge() {

return age;

}

public void setAge(String age) {

this.age = age;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getLable() {

return lable;

}

public void setLable(String lable) {

this.lable = lable;

}

}

三,创建Adapter继承BaseExpandableListAdapter

这里没有做任何优化,若想优化要判断convertView是否为空

并添加内部类viewholder

package com.lingzhuo.myexpandlistview.adapter;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseExpandableListAdapter;

import android.widget.TextView;

import com.lingzhuo.myexpandlistview.R;

import com.lingzhuo.myexpandlistview.moudel.Clazz;

import com.lingzhuo.myexpandlistview.moudel.Student;

import java.util.List;

/** * Created by heinika on 2015/8/26. */

public class ClazzAdapter extends BaseExpandableListAdapter {

private List mClazzs;

private LayoutInflater mInflater;

public ClazzAdapter(List mClazzs, LayoutInflater mInflater) {

this.mClazzs = mClazzs;

this.mInflater = mInflater;

}

@Override

public int getGroupCount() {

return mClazzs.size();

}

@Override

public int getChildrenCount(int groupPosition) {

return mClazzs.get(groupPosition).getStudents().size();

}

@Override

public Object getGroup(int groupPosition) {

return groupPosition;

}

@Override

public Object getChild(int groupPosition, int childPosition) {

return groupPosition;

}

@Override

public long getGroupId(int groupPosition) {

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) {

convertView =mInflater.inflate(R.layout.item_clazz,null);

Clazz clazz = mClazzs.get(groupPosition);

TextView textViewClassName = (TextView) convertView.findViewById(R.id.textview_clazz_name);

TextView textViewClassId = (TextView) convertView.findViewById(R.id.textview_clazz_id);

TextView textViewClassStudentNum = (TextView) convertView.findViewById(R.id.textview_clazz_student_num);

textViewClassName.setText(clazz.getClazzName());

textViewClassId.setText(clazz.getId());

textViewClassStudentNum.setText(clazz.getStudents().size()+"");

return convertView;

}

@Override

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

convertView =mInflater.inflate(R.layout.item_student,null);

Student student = mClazzs.get(groupPosition).getStudents().get(childPosition);

TextView textViewStudentName = (TextView) convertView.findViewById(R.id.textview_student_name);

TextView textViewSdutentAge = (TextView) convertView.findViewById(R.id.textview_student_age);

TextView textViewStudentSex = (TextView) convertView.findViewById(R.id.textview_clazz_student_sex);

textViewStudentName.setText(student.getName());

textViewSdutentAge.setText(student.getAge());

textViewStudentSex.setText(student.getSex());

return convertView;

}

@Override

public boolean isChildSelectable(int groupPosition, int childPosition) {

return false;

}

}

四,在activity中添加ExpandaListView.setAdapter

package com.lingzhuo.myexpandlistview;

import android.app.Activity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.widget.ExpandableListView;

import com.lingzhuo.myexpandlistview.adapter.ClazzAdapter;

import com.lingzhuo.myexpandlistview.moudel.Clazz;

import com.lingzhuo.myexpandlistview.moudel.Student;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends Activity {

private List clazzs;

private ExpandableListView mExpandableListView;

private LayoutInflater inflater;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mExpandableListView = (ExpandableListView) findViewById(R.id.mExpandableListView);

initClazzs();

inflater = getLayoutInflater();

ClazzAdapter adapter = new ClazzAdapter(clazzs,inflater);

mExpandableListView.setAdapter(adapter);

}

private void initClazzs() {

clazzs = new ArrayList();

Clazz clazz1 = new Clazz("class1","201501");

List students = new ArrayList();

Student student0 = new Student("zhangsan","19","男","。。。。。。");

Student student1 = new Student("zhangsan","19","男","。。。。。。");

Student student2 = new Student("zhangsan","19","男","。。。。。。");

Student student3 = new Student("zhangsan","19","男","。。。。。。");

students.add(student0);

students.add(student1);

students.add(student2);

students.add(student3);

clazz1.setStudents(students);

Clazz clazz2 = new Clazz("class2","201502");

clazz2.setStudents(students);

Clazz clazz3 = new Clazz("class3","201503");

clazz3.setStudents(students);

Clazz clazz4 = new Clazz("class4","201504");

clazz4.setStudents(students);

clazzs.add(clazz1);

clazzs.add(clazz2);

clazzs.add(clazz3);

clazzs.add(clazz4);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值