2010年8月22日日曜日

Android Expandable List を使う

一番簡単なのは、SimpleExpandableListAdapter を使う方法。

ApiDemo の ExpandableList3.java がまさにそのデモ。



ExpandableListActivity を継承した Activity を作ります。

  1. import android.app.ExpandableListActivity;  
  2. import android.os.Bundle;  
  3. import android.widget.ExpandableListAdapter;  
  4. import android.widget.SimpleExpandableListAdapter;  
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.HashMap;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10.   
  11. public class ExpandableListTest extends ExpandableListActivity {  
  12.     private static final String NAME = "NAME";  
  13.     private static final String IS_EVEN = "IS_EVEN";  
  14.           
  15.     private ExpandableListAdapter mAdapter;  
  16.           
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.   
  21.         // 親要素  
  22.         List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();  
  23.         // 子要素  
  24.         List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();  
  25.     
  26.         // 値を設定  
  27.         for (int i = 0; i < 20; i++) {  
  28.             // 親要素の値を設定  
  29.             Map<String, String> curGroupMap = new HashMap<String, String>();  
  30.             curGroupMap.put(NAME, "Group " + i);  
  31.             groupData.add(curGroupMap);  
  32.      
  33.             // 子要素の値を設定              
  34.             List<Map<String, String>> children = new ArrayList<Map<String, String>>();  
  35.             for (int j = 0; j < 15; j++) {  
  36.                 Map<String, String> curChildMap = new HashMap<String, String>();  
  37.                 curChildMap.put(NAME, "Child " + j);  
  38.                 curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");  
  39.                 children.add(curChildMap);  
  40.             }  
  41.             childData.add(children);  
  42.         }  
  43.               
  44.         // adapter 設定  
  45.         mAdapter = new SimpleExpandableListAdapter(  
  46.             this,  
  47.             // 親要素  
  48.             groupData,  
  49.             // 親要素のレイアウト  
  50.             android.R.layout.simple_expandable_list_item_1,  
  51.             // 親要素のListで、表示するMapのKey  
  52.             new String[] { NAME },  
  53.             // 親要素レイアウト内での文字を表示する TextView の ID  
  54.             new int[] { android.R.id.text1 },  
  55.             // 子要素  
  56.             childData,  
  57.             // 子要素のレイアウト  
  58.             android.R.layout.simple_expandable_list_item_2,  
  59.             // 子要素のListで、表示するMapのKey  
  60.             new String[] { NAME, IS_EVEN },  
  61.             // 子要素レイアウト内での文字を表示する TextView の ID  
  62.             new int[] { android.R.id.text1, android.R.id.text2 }  
  63.         );  
  64.         setListAdapter(mAdapter);  
  65.     }  
  66. }  

4 件のコメント:

  1. hi, got confuse how to start it,can you
    send me the source code, i really need this, thnk u
    id:edwardpark.1997@gmail.com

    返信削除
  2. はじめまして、きがんと申します。
    ExpandableListActivity を使用しようとしたのですが、
    このアクティビティ自身が起動しなくて困っています。
    Android1.5では動くのにAndroid1.6では関数の中にすら入る事ができませんでした。
    何か、Android1.6で同じような動きをするようなものって分かりませんでしょうか?

    返信削除
  3. k_iwata さん

    はじめまして。
    よく状況がつかめないのですが、どの関数のことですか?
    ExpandableListActivity は最新のOSでも使えますよ。

    返信削除
  4. ご回答ありがとうございます。

    自分がやろうとしていた事が親レイアウトをXMLで指定して作業を行うというものだったので、
    SimpleExpandableListAdapter で対応取れていないものだったから落ちていたようでした。

    返信削除