ExpandListView(可展開列表視圖) 簡單使用 //描述:像多個ListView垂直排在一塊兒同樣 //res/layout 佈局界面有3個xml文件佈局 //有4個類 1 -- Student 2 -- QingFengClass -- 班級類 3 -- MyAdapter -- 適配器類 4 -- MainActivity //在res/drawable-hdpi放入須要的頭像 圖片 一、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> -------------------------- 二、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> ----------------------- 三、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> -------------------- 四、Student 類 -- 存放學生信息 代碼 public class Student { public int headRes;//頭像 -- 圖片 -- 本地資源(drawable) public String name;//姓名 public Student(int headRes, String name) { this.headRes = headRes; this.name = name; } } -------------------- 五、QingFengClass 類 -- 存放班級信息 代碼 public class QingFengClass { // 班級名字 public String className; // 班級中的學生 public ArrayList<Student> students; public QingFengClass(String className, ArrayList<Student> students) { this.className = className; this.students = students; } } -------------------- 六、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; } } ------------------- 七、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); } }