什麼是ExpandableListActivity?
ExpandableListActivity是Activity類的子類,是一種具有多級條目顯示功能的Activity。
如何使用ExpandableListActivity?
1.讓MainActivity類繼承ExpandableListActivity類。
2.在activity_main佈局文件中定義ExpandableListView標籤。
3.創建一個佈局文件,用來定義一級條目的顯示格式。
4.創建一個佈局文件,用來定義二級條目的顯示格式。
5.創建多個List對象,分別爲一級條目和二級條目提供數據。
6.創建SimpleExpandableListAdapter對象,並設置給當前的ExpandableListActivity。
activity_main.java:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mycompany.expandablelistactivity.MainActivity">
<ExpandableListView
android:id="@id/android:list"
android:drawSelectorOnTop="false"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/groupTo"
android:textSize="25sp"
android:paddingLeft="30sp"
android:text="No data"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/childTo"
android:text="No data"
android:paddingLeft="30sp"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java:
package com.mycompany.expandablelistactivity;
import android.app.ExpandableListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 定義一個List,該List對象爲一級條目提供數據
List<Map<String,String>> groups = new ArrayList<Map<String,String>>();
Map<String, String> group1 = new HashMap<String, String>();
group1.put("group", "河南");
Map<String, String> group2 = new HashMap<String, String>();
group2.put("group", "山東");
groups.add(group1);
groups.add(group2);
// 定義一個List,該List對象爲第一個一級條目提供二級條目數據
List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
Map<String, String> child1Data1 = new HashMap<String, String>();
child1Data1.put("child", "鄭州");
Map<String, String> child1Data2 = new HashMap<String, String>();
child1Data2.put("child", "洛陽");
Map<String, String> child1Data3 = new HashMap<String, String>();
child1Data3.put("child", "南陽");
child1.add(child1Data1);
child1.add(child1Data2);
child1.add(child1Data3);
// 定義一個List,該List對象爲第二個一級條目提供二級條目數據
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2Data = new HashMap<String, String>();
child2Data.put("child", "濟南");
child2.add(child2Data);
// 定義一個List,該List對象用來存儲所有的二級條目數據
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(child1);
childs.add(child2);
// 生成一個SimpleExpandableListAdapter對象
// 參數列表:
// 1.上下文對象
// 2.一級條目的數據
// 3.用來設置一級條目樣式的佈局文件
// 4.指定一級條目數據的key
// 5.指定一級條目數據顯示控件的id
// 6.二級條目的數據
// 7.用來設置二級條目樣式的佈局文件
// 8.指定二級條目數據的key
// 9.指定二級條目數據顯示控件的id
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this, groups, R.layout.group,
new String[]{"group"}, new int[]{R.id.groupTo},
childs, R.layout.child,
new String[]{"child"}, new int[]{R.id.childTo});
// 將adapter設置給當前的ExpandableListActivity
setListAdapter(adapter);
}
}