Android实战简易教程(树形组件:ExpandableListView显示和动态添加删除)

ListView组件可以为用户提供列表的显示功能,但是如果想对这些列表数据进行分组管理,则需要使用android.widget.ExpandableListView组件完成。

与ListView组件一样,如果想要进行数据显示的设置,也需要一个适配器类,但是此时不再继承之前的BaseAdapter,而是继承BaseExpandableListAdapter类完成,此类为抽象类,所以要实现其中的所有抽象方法。

一、创建ExpandableListView

1.定义适配器类-MyExpandableListAdapter.java

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import android.content.Context;  
  4. import android.view.Gravity;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.AbsListView;  
  8. import android.widget.BaseExpandableListAdapter;  
  9. import android.widget.TextView;  
  10.   
  11. public class MyExpandableListAdapter extends BaseExpandableListAdapter {  
  12.     public String[] groupStrings = { "我的好友""我的家人""同事""狐朋狗友" };  
  13.     public String[][] childStrings = { { "瓜瓜""太湖""老皮""磨叽" },  
  14.             { "大姐""肥肥""二姐""爸爸" }, { "张工""程序猿" }, { "大鹏""二妞" } };  
  15.     private Context context;  
  16.   
  17.     MyExpandableListAdapter(Context context) {  
  18.         this.context = context;  
  19.     }  
  20.   
  21.     public int getGroupCount() {// 自动覆写  
  22.         return this.groupStrings.length;  
  23.     }  
  24.   
  25.     public int getChildrenCount(int groupPosition) {// 自动覆写  
  26.         return childStrings[groupPosition].length;  
  27.     }  
  28.   
  29.     public Object getGroup(int groupPosition) {// 自动覆写  
  30.         return groupStrings[groupPosition];  
  31.     }  
  32.   
  33.     public Object getChild(int groupPosition, int childPosition) {// 自动覆写  
  34.         return childStrings[groupPosition][childPosition];  
  35.     }  
  36.   
  37.     public long getGroupId(int groupPosition) {// 自动覆写  
  38.         return groupPosition;  
  39.     }  
  40.   
  41.     public long getChildId(int groupPosition, int childPosition) {// 自动覆写  
  42.         return childPosition;  
  43.     }  
  44.   
  45.     public boolean hasStableIds() {// 自动覆写  
  46.         return true;  
  47.     }  
  48.   
  49.     public View getGroupView(int groupPosition, boolean isExpanded,  
  50.             View convertView, ViewGroup parent) {// 自动覆写  
  51.         TextView textView = buildTextView();  
  52.         textView.setText(this.getGroup(groupPosition).toString());  
  53.         return textView;  
  54.     }  
  55.   
  56.     private TextView buildTextView() {  
  57.         AbsListView.LayoutParams params = new AbsListView.LayoutParams(  
  58.                 ViewGroup.LayoutParams.FILL_PARENT, 35);// 指定布局参数  
  59.         TextView textView = new TextView(this.context);// 创建TextView  
  60.         textView.setLayoutParams(params);// 设置布局参数  
  61.         textView.setTextSize(15.0f);  
  62.         textView.setGravity(Gravity.LEFT);// 左对齐  
  63.         textView.setPadding(40833);// 间距  
  64.         return textView;  
  65.     }  
  66.   
  67.     public View getChildView(int groupPosition, int childPosition,  
  68.             boolean isLastChild, View convertView, ViewGroup parent) {// 自动覆写  
  69.         TextView textView = buildTextView();  
  70.         textView.setText(getChild(groupPosition, childPosition).toString());  
  71.         return textView;  
  72.     }  
  73.   
  74.     public boolean isChildSelectable(int groupPosition, int childPosition) {// 自动覆写  
  75.         return true;  
  76.     }  
  77.   
  78. }  


2.定义布局文件:

[html]  view plain copy
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ExpandableListView  
  8.         android:id="@+id/elistview"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12. LinearLayout>  


3.定义MainActivity.java:

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.database.DataSetObserver;  
  5. import android.os.Bundle;  
  6. import android.view.ContextMenu;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.view.ContextMenu.ContextMenuInfo;  
  10. import android.widget.ExpandableListAdapter;  
  11. import android.widget.ExpandableListView;  
  12. import android.widget.Toast;  
  13. import android.widget.ExpandableListView.OnChildClickListener;  
  14. import android.widget.ExpandableListView.OnGroupClickListener;  
  15. import android.widget.ExpandableListView.OnGroupCollapseListener;  
  16. import android.widget.ExpandableListView.OnGroupExpandListener;  
  17.   
  18. public class MainActivity extends Activity {  
  19.     private ExpandableListView expandableListView;  
  20.     private ExpandableListAdapter adapter;  
  21.       
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState); // 生命周期方法  
  24.         super.setContentView(R.layout.main); // 设置要使用的布局管理器  
  25.     expandableListView=(ExpandableListView)findViewById(R.id.elistview);  
  26.     adapter=new MyExpandableListAdapter(this);  
  27.     expandableListView.setAdapter(adapter);  
  28.     super.registerForContextMenu(this.expandableListView);//注册上下文菜单  
  29.     /* 
  30.      * 下面四个监听是其特有的 
  31.      */  
  32.     expandableListView.setOnChildClickListener(new OnChildClickListenerImpl());//子项单击事件  
  33.     expandableListView.setOnGroupClickListener(new OnGroupClickListenerImpl());//组项单击事件  
  34.     expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());//关闭分组事件  
  35.     expandableListView.setOnGroupExpandListener(new OnGroupExpandListenerImpl());//展开分组事件  
  36.   
  37.     }  
  38.     private class OnChildClickListenerImpl implements OnChildClickListener{  
  39.   
  40.         public boolean onChildClick(ExpandableListView parent, View v,  
  41.                 int groupPosition, int childPosition, long id) {  
  42.             Toast.makeText(MainActivity.this"子项被选中,groupPosition="+groupPosition+"childPosition="+childPosition, Toast.LENGTH_SHORT).show();  
  43.             return false;  
  44.         }  
  45.           
  46.     }  
  47.     private class OnGroupClickListenerImpl implements OnGroupClickListener{  
  48.   
  49.         public boolean onGroupClick(ExpandableListView parent, View v,  
  50.                 int groupPosition, long id) {  
  51.             Toast.makeText(MainActivity.this"组项被选中,groupPosition="+groupPosition, Toast.LENGTH_SHORT).show();  
  52.             return false;  
  53.         }  
  54.           
  55.     }  
  56.     private class OnGroupCollapseListenerImpl implements OnGroupCollapseListener{  
  57.   
  58.         public void onGroupCollapse(int groupPosition) {  
  59.             Toast.makeText(MainActivity.this"分组关闭,groupPosition="+groupPosition, Toast.LENGTH_SHORT).show();  
  60.               
  61.         }  
  62.           
  63.     }  
  64.     private class OnGroupExpandListenerImpl implements OnGroupExpandListener{  
  65.   
  66.         public void onGroupExpand(int groupPosition) {  
  67.             Toast.makeText(MainActivity.this"分组展开,groupPosition="+groupPosition, Toast.LENGTH_SHORT).show();  
  68.         }  
  69.           
  70.     }  
  71.     @Override  
  72.     public void onCreateContextMenu(ContextMenu menu, View v,  
  73.             ContextMenuInfo menuInfo) {  
  74.         super.onCreateContextMenu(menu, v, menuInfo);  
  75.         ExpandableListView.ExpandableListContextMenuInfo info=(ExpandableListView.ExpandableListContextMenuInfo)menuInfo;  
  76.           
  77.           
  78.     }  
  79. }  


4.运行实例:

二、动态添加和删除

结合List的add()和remove()方法实现动态添加和删除操作。

1.定义适配器类:

[html]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.view.Gravity;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.AbsListView;  
  10. import android.widget.BaseExpandableListAdapter;  
  11. import android.widget.TextView;  
  12.   
  13. //为expandable list view 提供内容的基类  
  14. public class InfoDetailsAdapter extends BaseExpandableListAdapter {  
  15.     Activity activity;  
  16.   
  17.     List<String> group;  
  18.     List<List<String>> child;  
  19.   
  20.     public InfoDetailsAdapter(Activity a, List<String> group,  
  21.             List<List<String>> child) {  
  22.         activity = a;  
  23.         this.group = group;  
  24.         this.child = child;  
  25.     }  
  26.   
  27.     // child method stub  
  28.   
  29.     public Object getChild(int groupPosition, int childPosition) {  
  30.         // TODO Auto-generated method stub  
  31.         // System.out.println("*******************"+child.get(groupPosition).get(childPosition));  
  32.         return child.get(groupPosition).get(childPosition);  
  33.     }  
  34.   
  35.     public long getChildId(int groupPosition, int childPosition) {  
  36.         // TODO Auto-generated method stub  
  37.         return childPosition;  
  38.     }  
  39.   
  40.     public int getChildrenCount(int groupPosition) {  
  41.         // TODO Auto-generated method stub  
  42.         return child.get(groupPosition).size();  
  43.     }  
  44.   
  45.     public View getChildView(int groupPosition, int childPosition,  
  46.             boolean isLastChild, View convertView, ViewGroup parent) {  
  47.         // TODO Auto-generated method stub  
  48.         String string = child.get(groupPosition).get(childPosition);  
  49.         return getGenericView(string);  
  50.     }  
  51.   
  52.     // group method stub  
  53.     public Object getGroup(int groupPosition) {  
  54.         // TODO Auto-generated method stub  
  55.         return group.get(groupPosition);  
  56.     }  
  57.   
  58.     public long getGroupId(int groupPosition) {  
  59.         // TODO Auto-generated method stub  
  60.         return groupPosition;  
  61.     }  
  62.   
  63.     public int getGroupCount() {  
  64.         // TODO Auto-generated method stub  
  65.         return group.size();  
  66.     }  
  67.   
  68.     public View getGroupView(int groupPosition, boolean isExpanded,  
  69.             View convertView, ViewGroup parent) {  
  70.         // TODO Auto-generated method stub  
  71.         String string = group.get(groupPosition);  
  72.         return getGenericView(string);  
  73.     }  
  74.   
  75.     // View stub to create Group/Children 's View  
  76.     public TextView getGenericView(String s) {  
  77.         // Layout parameters for the ExpandableListView  
  78.         AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
  79.                 ViewGroup.LayoutParams.FILL_PARENT, 64);  
  80.   
  81.         TextView text = new TextView(activity);  
  82.         text.setLayoutParams(lp);  
  83.         // Center the text vertically  
  84.         text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  85.         // Set the text starting position  
  86.         text.setPadding(36, 0, 0, 0);  
  87.   
  88.         text.setText(s);  
  89.         return text;  
  90.     }  
  91.   
  92.     public boolean hasStableIds() {  
  93.         // TODO Auto-generated method stub  
  94.         return false;  
  95.     }  
  96.   
  97.     public boolean isChildSelectable(int groupPosition, int childPosition) {  
  98.         // TODO Auto-generated method stub  
  99.         return true;  
  100.     }  
  101.   
  102. }  


2.main.xml:

[html]  view plain copy
  1. <p>xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/androida>"  
  3.     android:id="@+id/layout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >p><p>    <ExpandableListView  
  7.         android:id="@+id/expandList"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content" />p><p>LinearLayout>p>  

3.add.xml

[html]  view plain copy
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal" >  
  11.   
  12.         <TextView  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="姓名:" />  
  16.   
  17.         <EditText  
  18.             android:id="@+id/add_name"  
  19.             android:layout_width="200dip"  
  20.             android:layout_height="wrap_content" />  
  21.     LinearLayout>  
  22.   
  23.     <LinearLayout  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:orientation="horizontal" >  
  27.   
  28.         <TextView  
  29.             android:layout_width="wrap_content"  
  30.             android:layout_height="wrap_content"  
  31.             android:text="电话:" />  
  32.   
  33.         <EditText  
  34.             android:id="@+id/add_phone"  
  35.             android:layout_width="200dip"  
  36.             android:layout_height="wrap_content" />  
  37.     LinearLayout>  
  38.   
  39.     <LinearLayout  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:orientation="horizontal" >  
  43.   
  44.         <TextView  
  45.             android:layout_width="wrap_content"  
  46.             android:layout_height="wrap_content"  
  47.             android:text="性别:" />  
  48.   
  49.         <EditText  
  50.             android:id="@+id/add_sex"  
  51.             android:layout_width="200dip"  
  52.             android:layout_height="wrap_content" />  
  53.     LinearLayout>  
  54.   
  55.     <LinearLayout  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="wrap_content"  
  58.         android:orientation="horizontal" >  
  59.   
  60.         <TextView  
  61.             android:layout_width="wrap_content"  
  62.             android:layout_height="wrap_content"  
  63.             android:text="住址:" />  
  64.   
  65.         <EditText  
  66.             android:id="@+id/add_home"  
  67.             android:layout_width="200dip"  
  68.             android:layout_height="wrap_content" />  
  69.     LinearLayout>  
  70.   
  71.     <LinearLayout  
  72.         android:layout_width="wrap_content"  
  73.         android:layout_height="wrap_content"  
  74.         android:orientation="horizontal" >  
  75.   
  76.         <Button  
  77.             android:id="@+id/add_ok"  
  78.             android:layout_width="90dip"  
  79.             android:layout_height="wrap_content"  
  80.             android:text="OK" />  
  81.   
  82.         <Button  
  83.             android:id="@+id/add_no"  
  84.             android:layout_width="90dip"  
  85.             android:layout_height="wrap_content"  
  86.             android:text="NO" />  
  87.     LinearLayout>  
  88.   
  89. LinearLayout>  


4.delete.xml:

[html]  view plain copy
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal" >  
  11.   
  12.         <TextView  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="ID:" />  
  16.   
  17.         <EditText  
  18.             android:id="@+id/delete_id"  
  19.             android:layout_width="200dip"  
  20.             android:layout_height="wrap_content" />  
  21.     LinearLayout>  
  22.   
  23.     <LinearLayout  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:orientation="horizontal" >  
  27.   
  28.         <Button  
  29.             android:id="@+id/delete_ok"  
  30.             android:layout_width="90dip"  
  31.             android:layout_height="wrap_content"  
  32.             android:text="OK" />  
  33.   
  34.         <Button  
  35.             android:id="@+id/delete_no"  
  36.             android:layout_width="90dip"  
  37.             android:layout_height="wrap_content"  
  38.             android:text="NO" />  
  39.     LinearLayout>  
  40.   
  41. LinearLayout>  


5.MainActivity.java:

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.app.Activity;  
  6. import android.app.Dialog;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.Menu;  
  10. import android.view.MenuItem;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.ExpandableListView;  
  16. import android.widget.Toast;  
  17. import android.widget.ExpandableListView.OnChildClickListener;  
  18. import android.widget.ExpandableListView.OnGroupClickListener;  
  19.   
  20. public class MainActivity extends Activity {  
  21.   
  22.     public final static int MENU_ADD = Menu.FIRST;  
  23.     public final static int MENU_DELETE = Menu.FIRST + 1;  
  24.   
  25.     ExpandableListView expandList;  
  26.     InfoDetailsAdapter adapter;  
  27.   
  28.     Activity activity;  
  29.   
  30.     List group;  
  31.     List> child;  
  32.   
  33.     // 初始化group child内容  
  34.     public void initialData() {  
  35.         group = new ArrayList();  
  36.         child = new ArrayList>();  
  37.   
  38.         addInfo("肥肥"new String[] { "234""two 1""three 1" });  
  39.         addInfo("肥蛋"new String[] { "one 2""two 2""three 2" });  
  40.         addInfo("垃圾肥"new String[] { "one 3""two 3""three 3" });  
  41.     }  
  42.   
  43.     public void addInfo(String p, String[] c) {  
  44.         group.add(p);  
  45.         List item = new ArrayList();  
  46.         for (int i = 0; i < c.length; i++) {  
  47.             item.add(c[i]);  
  48.         }  
  49.         child.add(item);  
  50.     }  
  51.   
  52.     /** Called when the activity is first created. */  
  53.     @Override  
  54.     public void onCreate(Bundle savedInstanceState) {  
  55.         super.onCreate(savedInstanceState);  
  56.         setContentView(R.layout.main);  
  57.   
  58.         activity = this;  
  59.   
  60.         expandList = (ExpandableListView) findViewById(R.id.expandList);  
  61.   
  62.         // 初始化各级元素  
  63.         initialData();  
  64.   
  65.         // 适配器内容  
  66.         adapter = new InfoDetailsAdapter(this, group, child);  
  67.   
  68.         expandList.setAdapter(adapter);  
  69.   
  70.         expandList.setOnGroupClickListener(new OnGroupClickListener() {  
  71.             public boolean onGroupClick(ExpandableListView arg0, View arg1,  
  72.                     int arg2, long arg3) {  
  73.                 // TODO Auto-generated method stub  
  74.                 System.out.println("The row id of the group clicked" + arg3);  
  75.                 Toast.makeText(activity, "[Group Click]:" + arg2,  
  76.                         Toast.LENGTH_SHORT).show();  
  77.                 return false;  
  78.             }  
  79.   
  80.         });  
  81.         expandList.setOnChildClickListener(new OnChildClickListener() {  
  82.             public boolean onChildClick(ExpandableListView arg0, View arg1,  
  83.                     int arg2, int arg3, long arg4) {  
  84.                 // TODO Auto-generated method stub  
  85.                 Toast.makeText(activity, "[Child Click]:" + arg2 + ":" + arg3,  
  86.                         Toast.LENGTH_SHORT).show();  
  87.                 return false;  
  88.             }  
  89.         });  
  90.     }  
  91.   
  92.     // 下述2个函数处理Menu按钮的事件  
  93.     public boolean onCreateOptionsMenu(Menu menu) {  
  94.         // TODO Auto-generated method stub  
  95.         menu.add(0, MENU_ADD, 0"     添加        ");  
  96.         menu.add(0, MENU_DELETE, 0"     删除        ");  
  97.   
  98.         return super.onCreateOptionsMenu(menu);  
  99.     }  
  100.   
  101.     public boolean onOptionsItemSelected(MenuItem item) {  
  102.         switch (item.getItemId()) {  
  103.         case MENU_ADD:  
  104.             Log.i("""FRIEND_ID");  
  105.             createDialogAdd();  
  106.             dialogAdd.show();  
  107.             break;  
  108.         case MENU_DELETE:  
  109.             Log.i("""FRIEND_ID");  
  110.             createDialogDelete();  
  111.             dialogDelete.show();  
  112.             break;  
  113.         }  
  114.         return super.onOptionsItemSelected(item);  
  115.     }  
  116.   
  117.     EditText add_name, add_phone, add_sex, add_home;  
  118.     EditText delete_id;  
  119.   
  120.     Button add_ok, add_no;  
  121.     Button delete_ok, delete_no;  
  122.   
  123.     Dialog dialogAdd, dialogDelete;  
  124.   
  125.     public void createDialogAdd() {//创建对话框  
  126.         Log.i("""createDialogAdd");  
  127.         View viewAdd = this.getLayoutInflater().inflate(R.layout.add, null);  
  128.   
  129.         dialogAdd = new Dialog(this);  
  130.         dialogAdd.setContentView(viewAdd);  
  131.         dialogAdd.setTitle("输入新成员信息");  
  132.   
  133.         add_name = (EditText) viewAdd.findViewById(R.id.add_name);  
  134.         add_phone = (EditText) viewAdd.findViewById(R.id.add_phone);  
  135.         add_sex = (EditText) viewAdd.findViewById(R.id.add_sex);  
  136.         add_home = (EditText) viewAdd.findViewById(R.id.add_home);  
  137.   
  138.         add_ok = (Button) viewAdd.findViewById(R.id.add_ok);  
  139.         add_no = (Button) viewAdd.findViewById(R.id.add_no);  
  140.   
  141.         add_ok.setOnClickListener(new OnClickListener() {//点击确定  
  142.             public void onClick(View v) {  
  143.                 // TODO Auto-generated method stub  
  144.                 String[] data = { add_phone.getText().toString(),  
  145.                         add_sex.getText().toString(),  
  146.                         add_home.getText().toString() };  
  147.   
  148.                 addInfo(add_name.getText().toString(), data);  
  149.   
  150.                 dialogAdd.dismiss();  
  151.   
  152.                 adapter.notifyDataSetChanged();  
  153.             }  
  154.         });  
  155.   
  156.         add_no.setOnClickListener(new OnClickListener() {//取消  
  157.             public void onClick(View v) {  
  158.                 // TODO Auto-generated method stub  
  159.                 dialogAdd.dismiss();  
  160.             }  
  161.         });  
  162.     }  
  163.   
  164.     public void createDialogDelete() {//创建删除按钮  
  165.         View viewDelete = this.getLayoutInflater().inflate(R.layout.delete,  
  166.                 null);  
  167.   
  168.         dialogDelete = new Dialog(this);  
  169.         dialogDelete.setContentView(viewDelete);  
  170.         dialogDelete.setTitle("删除指定成员");  
  171.   
  172.         delete_id = (EditText) viewDelete.findViewById(R.id.delete_id);  
  173.         delete_ok = (Button) viewDelete.findViewById(R.id.delete_ok);  
  174.         delete_no = (Button) viewDelete.findViewById(R.id.delete_no);  
  175.   
  176.         delete_ok.setOnClickListener(new OnClickListener() {  
  177.             public void onClick(View v) {  
  178.                 // TODO Auto-generated method stub  
  179.   
  180.                 String id = delete_id.getText().toString();  
  181.   
  182.                 if (!id.equals("")) {  
  183.                     int i = Integer.parseInt(id);  
  184.                     group.remove(i);  
  185.                     child.remove(i);  
  186.   
  187.                     dialogDelete.dismiss();  
  188.   
  189.                     adapter.notifyDataSetChanged();  
  190.                 }  
  191.   
  192.             }  
  193.         });  
  194.   
  195.         delete_no.setOnClickListener(new OnClickListener() {  
  196.             public void onClick(View v) {  
  197.                 // TODO Auto-generated method stub  
  198.                 dialogDelete.dismiss();  
  199.             }  
  200.         });  
  201.     }  
  202.   
  203. }  


运行实例如下:

总结

expandableListView.setOnChildClickListener(new OnChildClickListenerImpl());//子项单击事件

expandableListView.setOnGroupClickListener(new OnGroupClickListenerImpl());//组项单击事件

expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());//关闭分组事件

expandableListView.setOnGroupExpandListener(new OnGroupExpandListenerImpl());//展开分组事件

四个特有的事件监听。

你可能感兴趣的:(Android)