现在的位置: 首页 > 综合 > 正文

ExpandableListActivity用法小结

2013年06月29日 ⁄ 综合 ⁄ 共 2408字 ⁄ 字号 评论关闭

1,首先必须在布局文件中定义一个ExpandableListView,id为系统提供的@id/android:list

2,其次创建一级条目对应的布局文件group

3,创建二级条目对应的布局文件child

4,加载ExpandableListView组件的Activity必须继承自ExpandableListActivity

具体代码如下:

package com.sun.expandable_list_activity_test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;

public class ExpandableListActivityTestActivity extends ExpandableListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       //加载含有ExpandableListView组件的布局文件
        setContentView(R.layout.main);

       创建一级条目的数据源
        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);
       

     //为第一个一级条目添加第一个二级条目
        Map<String,String> child1Data1 = new HashMap<String,String>();
        child1Data1.put("child", "child1Data1");
     //为第一个一级条目添加第二个二级条目
        Map<String,String> child1Data2 = new HashMap<String,String>();
        child1Data2.put("child", "child1Data2");
       

    //创建第一个二级条目数据源
        List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
        child1.add(child1Data1);
        child1.add(child1Data2);
      

   //为第二个一级条目添加第一个二级条目  
        Map<String,String> child2Data1 = new HashMap<String,String>();
        child2Data1.put("child", "child2Data1");

   //创建第二个二级条目数据源

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

//创建二级条目数据源
        List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
        childs.add(child1);
        childs.add(child2);
      

//创建SimpleExpandableListAdapter 对象,  

/**groups                一级条目数据源对象

*R.layout.group     存放一级条目的布局文件id

*new String[]{"group"}    数据源中与布局文件对应位置所对应数据的key值

*new int[]{R.id.groupTo} 布局文件中与数据源中数据对应的位置id

*

*/

        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, groups, R.layout.group, new String[]{"group"}, new int[]{R.id.groupTo}, childs, R.layout.child, new String[]{"child"}, new int[]{R.id.childTo});
        setListAdapter(adapter);
       
       
       
       
       
       
       
       
       
    }
}

 

 

【上篇】
【下篇】

抱歉!评论已关闭.