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