ExpandableList

一、ExpandableList

Activity代码:

public class ExpandableListActivityDemoActivity extends ExpandableListActivity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //声明第一个list对象

        List<Map<String,String>> groups=new ArrayList<Map<String,String>>();

        Map<String,String> map1=new HashMap<String,String>();

        map1.put("group", "中国");

        groups.add(map1);

        Map<String,String> map2=new HashMap<String,String>();

        map2.put("group", "日本");

        groups.add(map2);

       

      //为第一个一级条目提供二级条目的数据

        List<Map<String,String>> child1=new ArrayList<Map<String,String>>();

        Map<String,String> child1data=new HashMap<String,String>();

        child1data.put("child", "香港");

        child1.add(child1data);

        Map<String,String> child2data=new HashMap<String,String>();

        child2data.put("child", "甘肃");

        child1.add(child2data);

       

      //为第二个一级条目提供二级条目的数据

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

        Map<String,String> child22data=new HashMap<String,String>();

        child22data.put("child", "北海道");

        child2.add(child22data);

        Map<String,String> child21data=new HashMap<String,String>();

        child21data.put("child", "北海道");

        child2.add(child21data);

       

        List<List<Map<String,String>>> childs=new ArrayList<List<Map<String,String>>>();

        childs.add(child1);

        childs.add(child2);

       

       

        SimpleExpandableListAdapter ada=new SimpleExpandableListAdapter(

                      this,

                      groups, R.layout.groups, new String[]{"group"}, new int[]{R.id.groups},

                      childs,R.layout.child,new String[]{"child"},new int[]{R.id.child}

                      );

        setListAdapter(ada);        

    }

 

       @Override

       public boolean onChildClick(ExpandableListView parent, View v,

                     int groupPosition, int childPosition, long id) {

              // TODO Auto-generated method stub

              Toast.makeText(this, groupPosition+"|"+childPosition, Toast.LENGTH_LONG).show();

              return super.onChildClick(parent, v, groupPosition, childPosition, id);

       }

   

}

groups.xml代码:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:id="@+id/groups" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="No Data"

    android:padding="15px"

    android:layout_marginLeft="35px"

    />

</LinearLayout>

child.xml代码:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:id="@+id/child" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="No Data"

    android:padding="10px"

    />

</LinearLayout>

main.xml代码:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:paddingLeft="8dp"

    android:paddingRight="8dp"

    >

<ExpandableListViewandroid:id="@id/android:list"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"    

    />

</LinearLayout>

 

posted @ 2011-08-29 23:07  rorshach  阅读(140)  评论(0编辑  收藏  举报