ExpandListView(可展开列表视图) 简单使用
//描述:像多个ListView垂直排在一起一样
//res/layout 布局界面有3个xml文件布局
//有4个类 1 -- Student
2 -- QingFengClass -- 班级类
3 -- MyAdapter -- 适配器类
4 -- MainActivity
//在res/drawable-hdpi放入需要的头像 图片
1、activity_main.xml文件 布局
代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
--------------------------
2、parent_item.xml文件 布局
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/class_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#A9A9A9"
android:textSize="40sp"
android:gravity="center"/>
</LinearLayout>
-----------------------
3、child_item.xml文件 布局
代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/head_img"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="120dp"/>
<TextView
android:id="@+id/name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#A9A9A9"
android:textSize="30sp"
android:layout_toRightOf="@+id/head_img"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
--------------------
4、Student 类 -- 存放学生信息
代码
public class Student {
public int headRes;//头像 -- 图片 -- 本地资源(drawable)
public String name;//姓名
public Student(int headRes, String name) {
this.headRes = headRes;
this.name = name;
}
}
--------------------
5、QingFengClass 类 -- 存放班级信息
代码
public class QingFengClass {
// 班级名字
public String className;
// 班级中的学生
public ArrayList<Student> students;
public QingFengClass(String className, ArrayList<Student> students) {
this.className = className;
this.students = students;
}
}
--------------------
6、MyAdapter 类 -- 继承BaseExpandableListAdapter 类
代码
public class MyAdapter extends BaseExpandableListAdapter {
private List<QingFengClass> list;
public MyAdapter(List<QingFengClass> list) {
this.list = list;
}
@Override
public int getGroupCount() {
return this.list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
QingFengClass qfc_list = list.get(groupPosition);
return qfc_list.students.size();
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
QingFengClass qfc_list = list.get(groupPosition);
return qfc_list.students.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
// 生成组中每个item视图
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.parent_item, null);
}
TextView class_view = (TextView) convertView
.findViewById(R.id.class_textview);
QingFengClass qfc_list = list.get(groupPosition);
class_view.setText(qfc_list.className);
return convertView;
}
//组中的某一个子view视图
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item, null);
}
ImageView img_head = (ImageView) convertView.findViewById(R.id.head_img);
TextView text_name = (TextView) convertView.findViewById(R.id.name_textview);
ArrayList<Student> qfc_list = list.get(groupPosition).students;
Student child_student = qfc_list.get(childPosition);
img_head.setImageResource(child_student.headRes);
text_name.setText(child_student.name);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
-------------------
7、MainActivity 类
代码
public class MainActivity extends Activity {
private MyAdapter adapter;
private ExpandableListView expandable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.expandable = (ExpandableListView) this.findViewById(R.id.expandableListView);
List<QingFengClass> qingfengclass_list = new ArrayList<QingFengClass>();
for(int i = 0;i<10;i++){//10个班级
ArrayList<Student> students = new ArrayList<Student>();
for(int j = 0;j<30;j++){//每个班级30个学生
Student student = new Student(R.drawable.student,"千峰" + i + "班" + j + "同学");
students.add(student);
}
QingFengClass class_Obejct = new QingFengClass(i + "班",students);
qingfengclass_list.add(class_Obejct);
}
adapter = new MyAdapter(qingfengclass_list );
expandable.setAdapter(adapter);
}
}
转载于:https://my.oschina.net/u/2542711/blog/603856