Android 学习笔记之ExpandableListView UI的简单用法

ExpandableListView是一个父子集的显示方式,

1、主界面

 1 public class ExpListViewDome extends Activity {
 2 
 3     private ExpandableListView Exp_ListView;
 4     private String[] mGroups;
 5     private List<List<String>> 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) this
13                 .findViewById(R.id.exp_ListView);
14         mGroups = new String[2];
15         mChilds = new ArrayList<List<String>>();
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<List<String>>();
35         List<String> list1 = new ArrayList<String>();
36         list1.add("啦啦啦");
37         list1.add("哈哈哈");
38         list1.add("嘎嘎嘎");
39         list1.add("呵呵呵");
40         List<String> list2 = new ArrayList<String>();
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<List<String>> mChilds;
 5     private String[] mGroups;
 6 
 7     public ExpListView_Adapter(Context context, List<List<String>> 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     @Override
17     public Object getChild(int groupPosition, int childPosition) {
18         // TODO Auto-generated method stub
19         return mChilds.get(groupPosition).get(childPosition);
20     }
21 
22     @Override
23     public long getChildId(int groupPosition, int childPosition) {
24         // TODO Auto-generated method stub
25         return childPosition;
26     }
27 
28     @Override
29     public View getChildView(int groupPosition, int childPosition,
30             boolean isLastChild, View convertView, ViewGroup parent) {
31         // TODO Auto-generated method stub
32         // 获取子窗口显示数据
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     @Override
46     public int getChildrenCount(int groupPosition) {
47         // TODO Auto-generated method stub
48         return mChilds.get(groupPosition).size();
49     }
50 
51     @Override
52     public Object getGroup(int groupPosition) {
53         // TODO Auto-generated method stub
54         return mGroups[groupPosition];
55     }
56 
57     @Override
58     public int getGroupCount() {
59         // TODO Auto-generated method stub
60         return mGroups.length;
61     }
62 
63     @Override
64     public long getGroupId(int groupPosition) {
65         // TODO Auto-generated method stub
66         return groupPosition;
67     }
68 
69     @Override
70     public View getGroupView(int groupPosition, boolean isExpanded,
71             View convertView, ViewGroup parent) {
72         // TODO Auto-generated method stub
73         // 获取父窗口显示数据
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     @Override
87     public boolean hasStableIds() {
88         // TODO Auto-generated method stub
89         return false;
90     }
91 
92     @Override
93     public boolean isChildSelectable(int groupPosition, int childPosition) {
94         // TODO Auto-generated method stub
95         //true   为子窗口有点击事件
96         return true;
97     }
View Code
原文地址:https://www.cnblogs.com/wjdawx/p/3229557.html