ExpendableListActivity控件的使用

来源:互联网 发布:mac os10.8.5升级10.9 编辑:程序博客网 时间:2024/06/05 01:03
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ExpandableListView 
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </ExpandableListView>
    
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No data" />


</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
     <TextView 
        android:id="@+id/childTo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10sp"
        />
     
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
    <TextView 
        android:id="@+id/groupTo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10sp"
        />


</LinearLayout>

----------------------------------------------------------------------------------------------------------------------------------------------

package com.example.expandablelistactivitytest;


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.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleExpandableListAdapter;






/*
 * 主要讲解的是ExpandableListActivity的例子
 * 是ListActivity的增强版
 * 
 * */
public class MainActivity extends ExpandableListActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

List<HashMap<String,String>> groups=new ArrayList<HashMap<String,String>>();
HashMap<String,String> group1=new HashMap<String,String>();
group1.put("group", "group1");
HashMap<String,String> group2=new HashMap<String,String>();
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);



List<HashMap<String,String>> child1=new ArrayList<HashMap<String,String>>();
HashMap<String,String> chid11data=new HashMap<String,String>();
chid11data.put("child", "child11");
HashMap<String,String> child12data=new HashMap<String,String>();
child12data.put("child", "child12");

List<HashMap<String,String>> child2=new ArrayList<HashMap<String,String>>();
HashMap<String,String> chid21data=new HashMap<String,String>();
chid21data.put("child", "child21");
HashMap<String,String> child22data=new HashMap<String,String>();
child22data.put("child", "child22");

List<List<HashMap<String,String>>> childs=new ArrayList<List<HashMap<String,String>>>(); 
childs.add(child1);
childs.add(child2);

/*
* 上面都是数据准备的工作
* SimpleExpandableListAdapter 这个构造函数有9个参数
* 第一个参数:上下文对象
* 第二个参数:一级条目的数据
* 第三个参数:以及条目显示的样式
* 第四个参数:以及条目的Map中的key
* 第五个参数 一级条目显示在控件控件中ID
* 第六个参数    二级条目的数据’
* 第七个参数    二级条目的样式
* 第八个参数  二级条目的key
* 第九个参数  二级条目显示在控件中的ID
* */


SimpleExpandableListAdapter sela=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(sela);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

0 0