ExpandableListView总结
在实现类似通信录,等带有两层或多层组织架构的列表功能时,会使用到ExpandableListView,他是ListView的子类,使用方式也和ListView大同小异,这里做一个总结。
关键类:
1、SimpleExpandableListAdapter
2、SimpleCursorTreeAdapter
3、BaseExpandableListAdapter
4、ExpandableListActivity
SimpleExpandableListAdapter
构造方法:
SimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData, //组数据
int groupLayout,//组数据使用的布局文件
String[] groupFrom, //组数据所使用的key数组
int[] groupTo,//组数据放置的控件id,例如TextView
List<? extends List<? extends Map<String,?>>> childData,//子元素数据
int childLayout, //子元素使用的布局文件
String[] childFrom, //子元素数据所使用的key数组
int[] childTo)//子元素数据放置的控件id,例如TextView
例如:生成一条组数据,组名为“group1”
Map<String, String> map1 = new HashMap<>();
map1.put("group", "group1");//其中"group"只是一个索引key
集合保存所有的组数据
List<Map<String, String>> groups = new ArrayList<>();
保存一条子元素数据,和组数据一样
Map<String, String> cMap1 = new HashMap<>();
cMap1.put("child","child1_data1");
child1.add(cMap1);//添加到集合
保存一个组的子元素数据
List<Map<String, String>> child1 = new ArrayList<>();
保存所有组的子元素数据
List<List<Map<String, String>>> childs = new ArrayList<>();
构造方法
SimpleExpandableListAdapter adapter1 = new SimpleExpandableListAdapter(this,
groups, R.layout.expandable_group, new String[]{"group"}, new int[]{R.id.text_group},
childs, R.layout.expandable_child, new String[]{"child"}, new int[]{R.id.text_child});
BaseExpandableListAdapter
继承BaseExpandableListAdapter实现自己的Adapter:
publicclassMyAdapterextendsBaseExpandableListAdapter{String[]groups=newString[]{"Group_111","Group_222","Group_333"};String[][]childs=newString[][]{{"g1_c1","g1_c2","g1_c3"},{"g2_c1","g2_c2","g2_c3"},{"g3_c1","g3_c2","g3_c3"}};@OverridepublicintgetGroupCount(){returngroups.length;}@OverridepublicintgetChildrenCount(intgroupPosition){returnchilds[groupPosition].length;}@OverridepublicObjectgetGroup(intgroupPosition){returngroups[groupPosition];}@OverridepublicObjectgetChild(intgroupPosition,intchildPosition){returnchilds[groupPosition][childPosition];}@OverridepubliclonggetGroupId(intgroupPosition){returngroupPosition;//这里直接返回组元素下标}@OverridepubliclonggetChildId(intgroupPosition,intchildPosition){returnchildPosition;//这里直接返回第二层元素下标}@OverridepublicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,ViewGroupparent){Viewview=View.inflate(MainActivity.this,R.layout.expandable_group,null);TextViewtext=(TextView)view.findViewById(R.id.text_group);text.setText(groups[groupPosition]);returnview;//实际项目中应该考虑convertView的重用}@OverridepublicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,ViewconvertView,ViewGroupparent){Viewview=View.inflate(MainActivity.this,R.layout.expandable_child,null);TextViewtext=(TextView)view.findViewById(R.id.text_child);text.setText(childs[groupPosition][childPosition]);returnview;}@Override//组合子元素是否拥有稳定的id,底层的数据集改表是否影响该idpublicbooleanhasStableIds(){returnfalse;}@Override//子元素是否可选中,有点击反馈publicbooleanisChildSelectable(intgroupPosition,intchildPosition){returntrue;}}
ExpandableListActivity和ListActivity类似
写自己的Activity继承自该类
该类自带一个ExpandableListView。id为:“@android:id/list”(或者代码中为“list”)
可以有一个id为:“@android:id/empty”布局,作为adapter没数据时的替代布局
通常写自己的布局并满足上述条件,并加以扩展实现自己的需求
SimpleCursorTreeAdapter
不同于SimpleExpandableListAdapter数据源来自List<Map<?,?>>,SimpleCursorTreeAdapter数据源来自访问数据库返回的Cursor。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。