ExpandableListView所展示的界面像QQ的界面,一个LiistView点击之后又出现新的子view,如:
具体如何使用,下面通过一个实例展示:
其中一级View为calss,每个calss中包含若干学生,产生的效果如下图所示
1.首先创建一个layout
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/expandablelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ExpandableListView>
</RelativeLayout>
2.建立class,跟student中内容展示的layout文件
(1)clazz_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/clazz_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/clazz_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/clazz_student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
(2)student_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/student_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/student_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/student_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3.创建Clazz跟Students类
(1)Clazz.java
package com.example.administrator.myexpanablelistview;
import com.example.administrator.myexpanablelistview.Students;
import java.util.List;
/**
* Created by Administrator on 2015/8/26.
*/
public class Clazz {
private String clazzName;
private String calzzNum;
private List student;
public List<Students> getStudent() {
return student;
}
public void setStudent(List<Students> student) {
this.student = student;
}
public Clazz(String clazzName, String calzzNum) {
this.clazzName = clazzName;
this.calzzNum = calzzNum;
}
public String getClazzName() {
return clazzName;
}
public void setClazzName(String clazzName) {
this.clazzName = clazzName;
}
public String getCalzzNum() {
return calzzNum;
}
public void setCalzzNum(String calzzNum) {
this.calzzNum = calzzNum;
}
}
(2)Students.java
package com.example.administrator.myexpanablelistview;
/**
* Created by Administrator on 2015/8/26.
*/
public class Students {
private String name;
private String age;
private String sex;
private String label;
public Students(String name, String age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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 getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
4.创建ExpandableListView的适配器
注意此时继承的是BaseExpanableAdapter
package com.example.administrator.myexpanablelistview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2015/8/26.
*/
public class MyExpandablelistviewAdapter extends BaseExpandableListAdapter {
private LayoutInflater minflater;
private List<Clazz> mClazzes;
@Override
public int getGroupCount() {
return mClazzes.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mClazzes.get(groupPosition).getStudent().size();//得到班级的位置,再得到班里中的学生,再得到学生的数量
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return 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;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView =minflater.inflate(R.layout.clazz_item,null);
TextView clazzName= (TextView) convertView.findViewById(R.id.clazz_name);
TextView clazzNum= (TextView) convertView.findViewById(R.id.clazz_number);
TextView clazzStudent= (TextView) convertView.findViewById(R.id.clazz_student);
Clazz clazz=mClazzes.get(groupPosition);
clazzName.setText(clazz.getClazzName());
clazzNum.setText(clazz.getCalzzNum());
clazzStudent.setText("" + clazz.getStudent().size());
return convertView;
}
public MyExpandablelistviewAdapter(LayoutInflater minflater, List<Clazz> mClazzes) {
this.minflater = minflater;
this.mClazzes = mClazzes;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
convertView =minflater.inflate(R.layout.student_item,null);
TextView studentName= (TextView) convertView.findViewById(R.id.student_name);
TextView studentAge= (TextView) convertView.findViewById(R.id.student_age);
TextView studentsex= (TextView) convertView.findViewById(R.id.student_sex);
Clazz clazz=mClazzes.get(groupPosition);
List<Students> students=clazz.getStudent();
Students student=students.get(childPosition);
studentName.setText(student.getName());
studentAge.setText(student.getAge());
studentsex.setText(student.getSex());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
5.MainActivity
package com.example.administrator.myexpanablelistview;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private ExpandableListView mExpandableListView;
private List<Clazz> clazzs;
private List<Students> students;
private LayoutInflater mInflater;
private MyExpandablelistviewAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExpandableListView= (ExpandableListView) findViewById(R.id.expandablelistview);
initData();//数据初始化
mInflater=getLayoutInflater();//
mAdapter=new MyExpandablelistviewAdapter(mInflater,clazzs);
mExpandableListView.setAdapter(mAdapter);
}
private void initData(){//创建要添加的数据
clazzs=new ArrayList<>();
Clazz clazz1=new Clazz("一班","201501");
List<Students> students1= new ArrayList<>();
students1.add(new Students("张三","18","男"));
students1.add(new Students("李四","18","男"));
students1.add(new Students("王五","21","男"));
students1.add(new Students("赵六","19","男"));
clazz1.setStudent(students1);
clazzs.add(clazz1);
Clazz clazz2=new Clazz("二班","201502");
List<Students> students2= new ArrayList<>();
students2.add(new Students("张三","18","男"));
students2.add(new Students("李四","18","男"));
students2.add(new Students("王五","21","男"));
students2.add(new Students("赵六", "19", "男"));
clazz2.setStudent(students2);
clazzs.add(clazz2);
Clazz clazz3=new Clazz("三班","201503");
List<Students> students3= new ArrayList<>();
students3.add(new Students("张三","18","男"));
students3.add(new Students("李四","18","男"));
students3.add(new Students("王五","21","男"));
students3.add(new Students("赵六", "19", "男"));
clazz3.setStudent(students3);
clazzs.add(clazz3);
Clazz clazz4=new Clazz("四班","201504");
List<Students> students4= new ArrayList<>();
students4.add(new Students("张三","18","男"));
students4.add(new Students("李四","18","男"));
students4.add(new Students("王五","21","男"));
students4.add(new Students("赵六", "19", "男"));
clazz4.setStudent(students4);
clazzs.add(clazz4);
}
}
关于ExpandableListView中箭头的设置
1.通过Layout文件设置
1.在drawable中设置一个xml文件,用来放置箭头的图片。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 收缩状态-->
<item android:drawable="@drawable/a_right_arrow" android:state_expanded="false"
android:drawingCacheQuality="auto"
></item>
<!-- 展开状态-->
<item android:drawable="@drawable/a_xiala_arrow_2"
android:state_expanded="true"
android:drawingCacheQuality="auto"></item>
</selector>
2.要在layout布局的xml文件中的ExpandableListView中添加如下代码:
android:groupIndicator="@drawable/expand"//expand就是在drawable中刚设置的xml文件名
2.通过代码修改
1.要在groupitem中添加ImageView(用于添加收缩跟展开的箭头)。
<ImageView
android:id="@+id/image_expand"
android:layout_width="30dp"
android:layout_height="30dp" />
2.要在BaseExpandableListViewAdapter文件中的getGroupView的方法中添加如下代码:
ImageView mImageView= (ImageView) convertView.findViewById(R.id.image_expand);
if(isExpanded){
mImageView.setImageResource(R.drawable.a_xiala_arrow_2);//如果展开了,就添加下拉的箭头
}else {
mImageView.setImageResource(R.drawable.a_right_arrow);//如果未展开就添加向右的箭头
}