折り畳み式リストビューを表示する |
ここでは折り畳み式リストビューを表示する方法を確認していきます。
折り畳み式のリストビューを作成するには「 ExpandableListView 」クラスを使います。 サンプルでは親要素を3、子要素を3つずつ作成し、子要素をクリックしたときにトーストで押下された子データを表示するようになっています。 大まかな流れ 1.親要素となるアイテムリストを作成する(parentsList) 2.子要素となるアイテムリストを作成する(childrenList) 3.アダプターを作成する(SimpleExpandableListAdapterクラス) 4.アダプターをExpandableListViewにセットする 5.子をクリックしたときの動作を作成する(setOnChildClickListener) Listview2Activity.java package goodroid.sample.listview2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; import android.widget.Toast; public class Listview2Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ExpandableListView lv = (ExpandableListView)findViewById(R.id.list); List<Map<String, Object>> parentsList = new ArrayList<Map<String, Object>>(); List<List<Map<String, Object>>> childrenList = new ArrayList<List<Map<String, Object>>>(); // 親リストを3つ作成 for(int i = 1; i <= 3; i++){ Map<String, Object> parentData = new HashMap<String, Object>(); parentData.put("PARENT", "親" + i); parentsList.add(parentData); // 子リストの一時保存用 List<Map<String, Object>> childList = new ArrayList<Map<String,Object>>(); // 子リストを3つ作成 for(int j = 1; j <= 3; j++){ Map<String, Object> childData = new HashMap<String, Object>(); childData.put("CHILD", "子" + j); childList.add(childData); } childrenList.add(childList); } // アダプターを作成する SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, parentsList, android.R.layout.simple_expandable_list_item_1, new String[]{"PARENT"}, new int[]{android.R.id.text1}, childrenList, android.R.layout.simple_expandable_list_item_1, new String[]{"CHILD"}, new int[]{android.R.id.text1} ); // アダプターをExpandableListViewにセットする lv.setAdapter(adapter); // 子をクリックしたときの動作 lv.setOnChildClickListener( new ExpandableListView.OnChildClickListener() { @SuppressWarnings("unchecked") public boolean onChildClick( ExpandableListView parent, View v, int groupPosition, int childPosition, long id ) { // ▼クリックしたときに行う処理を記述する▼ // アダプターから子の情報を取得 ExpandableListAdapter adapter = parent.getExpandableListAdapter(); Map<String, Object>childMap = (Map<String, Object>)adapter.getChild( groupPosition, childPosition ); // トーストにクリックした子の情報を表示 Toast.makeText( Listview2Activity.this, childMap.get("CHILD").toString(), Toast.LENGTH_SHORT ).show(); // ▲クリックしたときに行う処理を記述する▲ return false; } } ); } } main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ExpandableListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 実行結果 ![]() ![]() ![]() 【関連記事】 ・配列のデータをリストビューに表示する ・クリックしたリストの場所を取得する ・長押ししたリストの場所を取得する |
6460 views | コメント:0 | 2012-06-22 |
コメント
|
|
まだこの記事にコメントはありません |