1 package com.lixu.qqfenzu; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import android.app.Activity; 9 import android.content.Context; 10 import android.graphics.Color; 11 import android.os.Bundle; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.view.ViewGroup; 16 import android.widget.Button; 17 import android.widget.EditText; 18 import android.widget.ExpandableListView; 19 import android.widget.ExpandableListView.OnGroupClickListener; 20 import android.widget.SimpleExpandableListAdapter; 21 import android.widget.TextView; 22 23 public class MainActivity extends Activity { 24 private ArrayList<HashMap<String, Object>> date; 25 private final String GROUP = "group"; 26 private final String CHILD = "child"; 27 EditText et; 28 MyExpandableListAdapter mExpandableListAdapter = null; 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.activity_main); 34 35 Button btn1 = (Button) findViewById(R.id.btn); 36 Button btn2 = (Button) findViewById(R.id.btn1); 37 et = (EditText) findViewById(R.id.et); 38 39 ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv); 40 41 btn1.setOnClickListener(new OnClickListener() { 42 43 @Override 44 public void onClick(View v) { 45 addGroup(2); 46 } 47 }); 48 btn2.setOnClickListener(new OnClickListener() { 49 50 @Override 51 public void onClick(View v) { 52 addChild(1); 53 } 54 }); 55 56 String[] group = { "家人", "朋友", "同事", "同学" }; 57 String[] name = { "张三", "李四", "王五", "狗儿" }; 58 59 date = new ArrayList<HashMap<String, Object>>(); 60 61 for (String n : group) { 62 HashMap<String, Object> map = new HashMap<String, Object>(); 63 64 map.put(GROUP, n); 65 66 ArrayList<String> child = new ArrayList<String>(); 67 for (String n1 : name) { 68 child.add(n1); 69 } 70 71 map.put(CHILD, child); 72 date.add(map); 73 74 } 75 76 mExpandableListAdapter = new MyExpandableListAdapter(this, null, 0, 0, null, null, null, 0, 0, null, null); 77 elv.setAdapter(mExpandableListAdapter); 78 // 去掉下拉小箭头 79 elv.setGroupIndicator(null); 80 81 // 展开0组 82 // elv.expandGroup(0); 83 // 收起1组 84 // elv.collapseGroup(1); 85 // 展开2组 86 // elv.expandGroup(2); 87 88 elv.setOnGroupClickListener(new OnGroupClickListener() { 89 90 @Override 91 public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3) { 92 return false; 93 } 94 }); 95 } 96 97 // 增加组 98 public void addGroup(int pos) { 99 String str = et.getText().toString(); 100 101 HashMap<String, Object> map = new HashMap<String, Object>(); 102 103 map.put(GROUP, str); 104 105 ArrayList<String> child = new ArrayList<String>(); 106 107 map.put(CHILD, child); 108 date.add(pos, map); 109 110 mExpandableListAdapter.notifyDataSetChanged(); 111 et.setText(""); 112 113 } 114 115 // 增加特定组内数据 116 public void addChild(int pos) { 117 String str = et.getText().toString(); 118 // 往第2组内添加数据 119 HashMap<String, Object> map = date.get(1); 120 ArrayList<String> list = (ArrayList<String>) map.get(CHILD); 121 list.add(pos, str); 122 123 mExpandableListAdapter.notifyDataSetChanged(); 124 et.setText(""); 125 126 } 127 128 private class MyExpandableListAdapter extends SimpleExpandableListAdapter { 129 private LayoutInflater flater = null; 130 131 public MyExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, 132 int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo, 133 List<? extends List<? extends Map<String, ?>>> childData, int childLayout, int lastChildLayout, 134 String[] childFrom, int[] childTo) { 135 super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData, 136 childLayout, lastChildLayout, childFrom, childTo); 137 flater = LayoutInflater.from(context); 138 } 139 140 @Override 141 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, 142 ViewGroup parent) { 143 if (convertView == null) 144 convertView = flater.inflate(android.R.layout.simple_list_item_1, null); 145 146 TextView tv = (TextView) convertView.findViewById(android.R.id.text1); 147 tv.setText(getChild(groupPosition, childPosition) + ""); 148 149 tv.setTextSize(20); 150 151 return convertView; 152 } 153 154 @Override 155 public int getChildrenCount(int groupPosition) { 156 157 ArrayList<String> items = (ArrayList<String>) date.get(groupPosition).get(CHILD); 158 return items.size(); 159 } 160 161 @Override 162 public int getGroupCount() { 163 return date.size(); 164 } 165 166 @Override 167 public Object getChild(int groupPosition, int childPosition) { 168 ArrayList<String> items = (ArrayList<String>) date.get(groupPosition).get(CHILD); 169 return items.get(childPosition); 170 } 171 172 @Override 173 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 174 if (convertView == null) 175 convertView = flater.inflate(android.R.layout.simple_list_item_1, null); 176 177 TextView tv = (TextView) convertView.findViewById(android.R.id.text1); 178 tv.setText(getGroup(groupPosition).toString()); 179 tv.setTextColor(Color.RED); 180 tv.setTextSize(30); 181 182 return convertView; 183 184 } 185 186 @Override 187 public Object getGroup(int groupPosition) { 188 HashMap<String, Object> items = date.get(groupPosition); 189 return items.get(GROUP) + ""; 190 } 191 192 } 193 194 }
xml文件:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.lixu.qqfenzu.MainActivity" > 6 7 <ExpandableListView 8 android:id="@+id/elv" 9 android:layout_width="match_parent" 10 android:layout_height="300dp" /> 11 12 <LinearLayout 13 android:id="@+id/linearLayout1" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_below="@id/elv" > 17 18 <EditText 19 android:id="@+id/et" 20 android:layout_width="100dp" 21 android:layout_height="wrap_content" > 22 23 <requestFocus /> 24 </EditText> 25 </LinearLayout> 26 27 <Button 28 android:id="@+id/btn1" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:layout_alignParentRight="true" 32 android:layout_alignTop="@+id/linearLayout1" 33 android:text="添加内容" /> 34 35 <Button 36 android:id="@+id/btn" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_alignBaseline="@+id/btn1" 40 android:layout_alignBottom="@+id/btn1" 41 android:layout_toLeftOf="@+id/btn1" 42 android:text="添加组" /> 43 44 </RelativeLayout>
运行效果图:
时间: 2024-10-12 17:50:51