android學習--ExpandableListActivity和SimpleExpandableListAdapter的使用

在佈局文件中聲明ExpandableListActivity控件main.xml

<ExpandableListView android:id="@id/android:list"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:drawSelectorOnTop="false"/>

 

<TextView android:id="@id/android:empty" 

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:text="No Data"/>

2在佈局文件中聲明group樣式group.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/groupTo"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:paddingLeft="60dp"

        android:paddingTop="1dp"

        android:paddingBottom="10dp"

        android:textSize="26sp"

        android:text="No Data"/> 

</LinearLayout>

3在佈局文件中聲明子項的樣式child.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/childTo"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:paddingLeft="50dp"

        android:paddingTop="5dp"

        android:paddingBottom="5dp"

        android:textSize="20sp"

        android:text="@string/nodata"/>

</LinearLayout>

4創建一個Activity ,繼承自ExpandableListActivity 

public class MainActivity extends ExpandableListActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

5爲group創建數據

List<Map<String, String>> groups = new ArrayList<Map<String, String>>();

Map<String, String> group1 = new HashMap<String, String>();

group1.put("group""group1");

Map<String, String> group2 = new HashMap<String, String>();

group2.put("group""group2");

groups.add(group1);

groups.add(group2);

6child創建數據

// 定義一個List,該List爲第一個一級條目提供二級條目的數據

List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();

Map<String, String> child1Data1 = new HashMap<String, String>();

child1Data1.put("child""child1Data1");

Map<String, String> child1Data2 = new HashMap<String, String>();

child1Data2.put("child""child1Data2");

child1.add(child1Data1);

child1.add(child1Data2); 

// 定義一個List,該List爲第二個一級條目提供二級條目的數據

List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();

Map<String, String> child2Data = new HashMap<String, String>();

child2Data.put("child""child2Data");

child2.add(child1Data1); 

// 定義一個List,該List用來存儲所有的二級條目的數據

List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

childs.add(child1);

childs.add(child2);

7生成一個SimpleExpandableListAdapter對象,設置給當前ExpandableListActivity

SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(

this, groups, R.layout.groupnew String[] { "group" },

new int[] { R.id.groupTo }, childs, R.layout.child,

new String[] { "child" }, new int[] { R.id.childTo });

//將SimpleExpandableListAdapter對象設置給當前的ExpandableListActivity

setListAdapter(adapter);

發佈了21 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章