ExpandableListView是一个父子集的显示方式,
1、主界面
1 public class ExpListViewDome extends Activity { 2 3 private ExpandableListView Exp_ListView; 4 private String[] mGroups; 5 private List
> mChilds; 6 private ExpandableListAdapter adapter; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) {10 super.onCreate(savedInstanceState);11 setContentView(R.layout.activity_explistviewdome);12 Exp_ListView = (ExpandableListView) this13 .findViewById(R.id.exp_ListView);14 mGroups = new String[2];15 mChilds = new ArrayList
>();16 getData();17 adapter = new ExpListView_Adapter(ExpListViewDome.this, mChilds,18 mGroups);19 Exp_ListView.setAdapter(adapter);20 21 //默认展开第一个父窗口22 Exp_ListView.expandGroup(0);23 24 }25 26 /**27 * 获取父窗口和子窗口的数据28 */29 public void getData() {30 31 mGroups[0] = "第一个老子";32 mGroups[1] = "第二个老子";33 34 mChilds = new ArrayList
>();35 List list1 = new ArrayList ();36 list1.add("啦啦啦");37 list1.add("哈哈哈");38 list1.add("嘎嘎嘎");39 list1.add("呵呵呵");40 List list2 = new ArrayList ();41 list2.add("巴巴巴");42 list2.add("恰恰恰");43 list2.add("呼呼呼");44 list2.add("哼哼哼");45 mChilds.add(list1);46 mChilds.add(list2);47 48 }
2、自定义适配器
1 public class ExpListView_Adapter extends BaseExpandableListAdapter { 2 3 private Context mContext; 4 private List
> mChilds; 5 private String[] mGroups; 6 7 public ExpListView_Adapter(Context context, List
> list, 8 String[] Groups) { 9 // TODO Auto-generated constructor stub]10 this.mContext = context;11 this.mChilds = list;12 this.mGroups = Groups;13 14 }15 16 @Override17 public Object getChild(int groupPosition, int childPosition) {18 // TODO Auto-generated method stub19 return mChilds.get(groupPosition).get(childPosition);20 }21 22 @Override23 public long getChildId(int groupPosition, int childPosition) {24 // TODO Auto-generated method stub25 return childPosition;26 }27 28 @Override29 public View getChildView(int groupPosition, int childPosition,30 boolean isLastChild, View convertView, ViewGroup parent) {31 // TODO Auto-generated method stub32 // 获取子窗口显示数据33 String str = mChilds.get(groupPosition).get(childPosition);34 // 设置数据显示样式35 TextView textView = new TextView(mContext);36 textView.setText(str);37 textView.setPadding(60, 0, 10, 10);38 textView.setGravity(Gravity.CENTER_VERTICAL);39 textView.setTextColor(Color.BLUE);40 textView.setTextSize(24);41 42 return textView;43 }44 45 @Override46 public int getChildrenCount(int groupPosition) {47 // TODO Auto-generated method stub48 return mChilds.get(groupPosition).size();49 }50 51 @Override52 public Object getGroup(int groupPosition) {53 // TODO Auto-generated method stub54 return mGroups[groupPosition];55 }56 57 @Override58 public int getGroupCount() {59 // TODO Auto-generated method stub60 return mGroups.length;61 }62 63 @Override64 public long getGroupId(int groupPosition) {65 // TODO Auto-generated method stub66 return groupPosition;67 }68 69 @Override70 public View getGroupView(int groupPosition, boolean isExpanded,71 View convertView, ViewGroup parent) {72 // TODO Auto-generated method stub73 // 获取父窗口显示数据74 String Title = mGroups[groupPosition];75 // 设置数据显示样式76 TextView textView = new TextView(mContext);77 textView.setText(Title);78 textView.setBackgroundColor(Color.GRAY);79 textView.setPadding(60, 0, 0, 0);80 textView.setTextColor(Color.BLUE);81 textView.setTextSize(30);82 83 return textView;84 }85 86 @Override87 public boolean hasStableIds() {88 // TODO Auto-generated method stub89 return false;90 }91 92 @Override93 public boolean isChildSelectable(int groupPosition, int childPosition) {94 // TODO Auto-generated method stub95 //true 为子窗口有点击事件96 return true;97 }