Android基礎之ExpandableListActivity

來源:互聯網
上載者:User

2011-04-26    12:51:55

ExpandableListActivity 與ExpandableListView的關係就向 ListActivity與ListView一樣總是一起出現的


先上個個





 


 


 





































1.首先在main.xml添加ExpandableListView

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /> <ExpandableListView<br /> android:id="@id/android:list" //ExpandableListView的ID為Android自訂,不可改變<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br />android:background="@drawable/background"/><br /></LinearLayout><br />


2.為父目錄建立布局檔案

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br /> xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"><br /> <TextView<br /> android:id="@+id/parent_group"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:paddingTop="8px"<br /> android:paddingLeft="50px"<br /> android:paddingBottom="6px"<br /> android:textColor="#ff00ff"<br /> android:textStyle="bold"/><br /></LinearLayout><br />



3.為子目錄建立布局檔案

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br /> xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:paddingLeft="20dp"<br /> android:orientation="horizontal"><br /> <ImageView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:paddingLeft="20px"<br /> android:paddingTop="4dp"<br /> android:background="@drawable/child_image"/><br /> <LinearLayout<br /> android:orientation="vertical"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"><br /> <TextView<br /> android:id="@+id/child_group"<br /> android:textSize="20sp"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"/><br /> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="地址:xx省xx市xx"/><br /> </LinearLayout><br /></LinearLayout><br />



4.DisplayExpandableList.java

package com.yin;<br />import java.util.ArrayList;<br />import java.util.HashMap;<br />import java.util.List;<br />import android.app.ExpandableListActivity;<br />import android.os.Bundle;<br />import android.widget.ExpandableListAdapter;<br />import android.widget.ExpandableListView;<br />import android.widget.SimpleExpandableListAdapter;<br />public class DisplayExpandableList extends ExpandableListActivity {<br />/** Called when the activity is first created. */<br />@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);<br />List<HashMap<String, String>> parent_groups = new ArrayList<HashMap<String, String>>();<br />String[] parent_group_names = {"我的好友","初中同學","高中同學","大學同學","黑名單"};<br />parent_groups = this.addParentItems(parent_groups, parent_group_names);<br />List<List<HashMap<String, String>>> child_groups = new ArrayList<List<HashMap<String, String>>>();<br />List<HashMap<String, String>> child_lists = new ArrayList<HashMap<String, String>>();<br />child_groups = this.addChildsItems(child_groups, child_lists,<br />"小華");<br />/**<br /> * SimpleExpandableListAdapter的參數那是相當的多啊<br /> * 參數 1:context<br /> * 2:父級目錄的資料<br /> * 3:父級目錄的布局檔案<br /> * 4: 夫級目錄的資料來源<br /> * 5:指定父級目錄顯示資料的控制項<br /> * 6:子級目錄的資料<br /> * 7:子級目錄的布局檔案<br /> * 8:子級目錄的資料來源<br /> * 9:指定子級目錄顯示資料的控制項<br /> */<br />SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(<br />this, parent_groups, R.layout.parent_layout,<br />new String[]{"parent_group"}, new int[]{R.id.parent_group},<br />child_groups, R.layout.child_layout, new String[]{"child_group"},<br />new int[]{R.id.child_group});<br />setListAdapter(adapter);<br />}<br />//為每個父母目錄下子目錄添加資料<br />public List<List<HashMap<String, String>>> addChildsItems(<br />List<List<HashMap<String, String>>> child_groups,<br />List<HashMap<String, String>> child_list, String item) {</p><p>child_groups = new ArrayList<List<HashMap<String, String>>>();<br />child_list = new ArrayList<HashMap<String, String>>();<br />HashMap<String, String> map = new HashMap<String, String>();<br />map.put("child_group", item);<br />child_list.add(map);<br />child_groups.add(child_list);<br />return child_groups;<br />}<br />//為父級目錄分組,添加標識<br />public List<HashMap<String, String>> addParentItems(<br />List<HashMap<String, String>> parent_groups, String[] parent_group_names) {<br />parent_groups = new ArrayList<HashMap<String, String>>();</p><p>for(int i=0;i<parent_group_names.length;i++){<br />HashMap<String, String> groups = new HashMap<String, String>();<br />groups.put("parent_group", parent_group_names[i]);<br />parent_groups.add(groups);<br />}</p><p>return parent_groups;<br />}<br />}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.