ExpandableListView を使ってみた

ExpandableListView は
java.lang.Object
  ↳ android.view.View
    ↳ android.view.ViewGroup
      ↳ android.widget.AdapterView<T extends android.widget.Adapter>
        ↳ android.widget.AbsListView
          ↳ android.widget.ListView
            ↳ android.widget.ExpandableListView
を使用します

Group のリストがありそれぞれが Child のリストを保持しています.
Group をクリックすRと Child のリストが開き,もう一度クリックするとリストが閉じられます.

というわけで,ExpandableListView をいじってみた.

サンプルコード
  1. public class ExpandableListViewSample extends Activity {  
  2.   private static final String KEY1 = "GROUP";  
  3.   private static final String KEY2 = "CHILD";  
  4.   
  5.   // 表示させる文字列  
  6.   private String[] GROUPS = {"Group1""Group2""Group3"};  
  7.   private String[][][] CHILDREN = {  
  8.       {{"Child11""Text11"}},   
  9.       {{"Child21""Text21"}, {"Child22""Text22"}},  
  10.       {{"Child31""Text31"}, {"Child32""Text32"}, {"Child33""Text33"}},   
  11.   };  
  12.   
  13.   @Override  
  14.   public void onCreate(Bundle savedInstanceState) {  
  15.     super.onCreate(savedInstanceState);  
  16.     setContentView(R.layout.expandablelistview_sample);  
  17.   
  18.     // 設定する文字列のリスト  
  19.     List<Map<String, String>> groupData =  
  20.         new ArrayList<Map<String, String>>();  
  21.     List<List<Map<String, String>>> childData =  
  22.         new ArrayList<List<Map<String, String>>>();  
  23.   
  24.     // リストに文字列を設定していく  
  25.     for (int i = 0; i < GROUPS.length; i++) {  
  26.       // 親要素の追加  
  27.       Map<String, String> curGroupMap =  
  28.           new HashMap<String, String>();  
  29.       groupData.add(curGroupMap);  
  30.       curGroupMap.put(KEY1, GROUPS[i]);  
  31.       curGroupMap.put(KEY2, "");  
  32.   
  33.       List<Map<String, String>> children =  
  34.           new ArrayList<Map<String, String>>();  
  35.       if (CHILDREN.length > i) {  
  36.         for (int j = 0; j < CHILDREN[i].length; j++) {  
  37.           // 子要素の追加  
  38.           Map<String, String> curChildMap =  
  39.               new HashMap<String, String>();  
  40.           children.add(curChildMap);  
  41.           curChildMap.put(KEY1, CHILDREN[i][j][0]);  
  42.           curChildMap.put(KEY2, CHILDREN[i][j][1]);  
  43.         }  
  44.       }  
  45.       childData.add(children);  
  46.     }  
  47.   
  48.     // ExpandbleListAdapter の作成  
  49.     ExpandableListAdapter adapter =  
  50.         new SimpleExpandableListAdapter(  
  51.             this,  
  52.             groupData,  
  53.             android.R.layout.simple_expandable_list_item_1,  
  54.             new String[] { KEY1, KEY2 },  
  55.             new int[] { android.R.id.text1, android.R.id.text2 },  
  56.             childData,  
  57.             android.R.layout.simple_expandable_list_item_2,  
  58.             new String[] { KEY1, KEY2 },  
  59.             new int[] { android.R.id.text1, android.R.id.text2 }  
  60.         );  
  61.   
  62.     ExpandableListView listView =   
  63.       (ExpandableListView) findViewById(R.id.ExpandableListView);  
  64.     // Adapter を設定  
  65.     listView.setAdapter(adapter);  
  66.       
  67.     // グループがクリックされた時に呼び出されるコールバックを登録  
  68.     listView.setOnGroupClickListener(new OnGroupClickListener() {  
  69.       @Override  
  70.       public boolean onGroupClick(ExpandableListView parent,  
  71.           View v, int groupPosition, long id) {  
  72.         // クリックされた時の処理  
  73.         return false;  
  74.       }        
  75.     });  
  76.       
  77.     // グループ内の項目がクリックされた時に呼び出されるコールバックを登録  
  78.     listView.setOnChildClickListener(new OnChildClickListener() {  
  79.       @Override  
  80.       public boolean onChildClick(ExpandableListView parent, View v,  
  81.           int groupPosition, int childPosition, long id) {  
  82.         // クリックされた時の処理  
  83.         return false;  
  84.       }        
  85.     });  
  86.   }    
  87. }  

さらに
setOnGroupCollapseListener でグループが閉じた時,
setOnGroupExpandListener でグループが開いた時
の処理もできる模様!

XMLリソース
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"   
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.   <TextView  
  7.     android:layout_width="fill_parent"  
  8.     android:layout_height="wrap_content"   
  9.     android:text="ExpandableListViewSample" />  
  10.   <ExpandableListView   
  11.     android:layout_width="fill_parent"  
  12.     android:layout_height="0dip"   
  13.     android:layout_weight="1"  
  14.     android:id="@+id/ExpandableListView">  
  15.   </ExpandableListView>  
  16. </LinearLayout>  
プログラムを実行すると...


グループを開いたとき

こんな感じ!


参考サイト http://developer.android.com/intl/ja/reference/android/widget/ExpandableListView.html

ListView で MultipleChoice モード

ListView で複数選択できるような List を作ることができます.

基本的には ListView や SingleChoice と同じ.
Adapter の作成時に Muliple Choice ようのレイアウトを設定するのと
setChoiceMode で ListView.CHOICE_MODE_MULTIPLEを設定するくらい
すると CheckBox 付きのリストが表示されます.

というわけで,MultipleChoiceList をいじってみた.

サンプルコード
  1. public class MultipleChoiceListViewSample extends Activity {  
  2.   @Override  
  3.   public void onCreate(Bundle savedInstanceState) {  
  4.     super.onCreate(savedInstanceState);  
  5.     setContentView(R.layout.listview_sample);  
  6.     ListView listView = (ListView)findViewById(R.id.ListView);  
  7.   
  8.     // アダプタの作成  
  9.     listView.setAdapter(new ArrayAdapter<String>(  
  10.         this,  
  11.         android.R.layout.simple_list_item_multiple_choice,  
  12.         DAYS)  
  13.     );  
  14.   
  15.     // フォーカスが当たらないよう設定  
  16.     listView.setItemsCanFocus(false);  
  17.   
  18.     // 選択の方式の設定  
  19.     listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);  
  20.       
  21.     for (int i = 1; i < 6; i++) {  
  22.       // 指定したアイテムがチェックされているかを設定  
  23.       listView.setItemChecked(i, true);  
  24.     }  
  25.       
  26.     // アイテムがクリックされたときに呼び出されるコールバックを登録  
  27.     listView.setOnItemClickListener(new OnItemClickListener() {  
  28.       @Override  
  29.       public void onItemClick(AdapterView<?> parent,  
  30.               View view, int position, long id) {  
  31.           // クリックされた時の処理  
  32.       }  
  33.     });  
  34.       
  35.     // 現在チェックされているアイテムを取得  
  36.     // チェックされてないアイテムは含まれない模様  
  37.     SparseBooleanArray checked = listView.getCheckedItemPositions();  
  38.     for (int i = 0; i < checked.size(); i++) {  
  39.       // チェックされているアイテムの key の取得  
  40.       int key = checked.keyAt(i);  
  41.       Log.v(getClass().getSimpleName(), "values: " + DAYS[key]);  
  42.     }  
  43.   }  
  44.     
  45.   // ListView に表示させる文字列  
  46.   private static final String[] DAYS = new String[] {  
  47.     "Sunday""Monday""Tuesday""Wednesday",  
  48.     "Thursday""Friday""Saturday"  
  49.   };  
  50. }  
XMLリソース
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"   
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.   <TextView android:layout_width="fill_parent"  
  7.     android:layout_height="wrap_content"   
  8.     android:text="ListSample" />  
  9.   <ListView   
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="0dip"   
  12.     android:layout_weight="1"  
  13.     android:id="@+id/ListView">  
  14.   </ListView>  
  15. </LinearLayout>  

実行すると...



こんな感じ!


参考サイト
http://developer.android.com/intl/ja/reference/android/widget/ListView.html

ListView で SingleChoice モード

ListView で一つしか選択できないような List を作ることができます.

基本的には ListView と同じ.
Adapter の作成時に Single Choice ようのレイアウトを設定するのと
setChoiceMode で ListView.CHOICE_MODE_SINGLEを設定するくらい
すると RadioButton 付きのリストが表示されます.

というわけで,SingleChoiceList をいじってみた

サンプルコード
  1. public class SingleChoiceListViewSample extends Activity {  
  2.   @Override  
  3.   public void onCreate(Bundle savedInstanceState) {  
  4.     super.onCreate(savedInstanceState);  
  5.     setContentView(R.layout.listview_sample);  
  6.     ListView listView = (ListView)findViewById(R.id.ListView);  
  7.   
  8.     // アダプタの作成  
  9.     listView.setAdapter(new ArrayAdapter<String>(  
  10.         this,  
  11.         android.R.layout.simple_list_item_single_choice,  
  12.         SIZES)  
  13.     );    
  14.           
  15.     // 選択の方式の設定  
  16.     listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);  
  17.     
  18.     // 指定したアイテムがチェックされているかを設定  
  19.     listView.setItemChecked(0true);  
  20.       
  21.     // アイテムがクリックされた時に呼び出されるコールバックを登録  
  22.     listView.setOnItemClickListener(new OnItemClickListener() {  
  23.         @Override  
  24.         public void onItemClick(AdapterView<?> parent,  
  25.                 View view, int position, long id) {  
  26.             // クリックされた時の処理  
  27.         }  
  28.     });  
  29.     
  30.     // 現在チェックされているアイテムの position を取得  
  31.     listView.getCheckedItemPosition();  
  32.   }  
  33.     
  34.   // ListView に表示させる文字列  
  35.   private static final String[] SIZES = new String[] {  
  36.       "XS(eXtra Small)""S(Small)""M(Medium)",  
  37.    "L(Large)""XL(eXtra Large)"  
  38.   };  
  39. }  

XMLリソース
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"   
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.   <TextView android:layout_width="fill_parent"  
  7.     android:layout_height="wrap_content"   
  8.     android:text="ListSample" />  
  9.   <ListView   
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="0dip"   
  12.     android:layout_weight="1"  
  13.     android:id="@+id/ListView">  
  14.   </ListView>  
  15. </LinearLayout>  

実行すると...



こんな感じ!


参考サイト
http://developer.android.com/intl/ja/reference/android/widget/ListView.html