标签:总结 expandablelistview expandablelistadapter expandablelistactivity
在实现类似通信录,等带有两层或多层组织架构的列表功能时,会使用到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:
public class MyAdapter extends BaseExpandableListAdapter{ String[] groups = new String[]{"Group_111","Group_222","Group_333"}; String[][] childs = new String[][]{ {"g1_c1","g1_c2","g1_c3"}, {"g2_c1","g2_c2","g2_c3"}, {"g3_c1","g3_c2","g3_c3"}}; @Override public int getGroupCount() { return groups.length; } @Override public int getChildrenCount(int groupPosition) { return childs[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return groups[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return childs[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition;//这里直接返回组元素下标 } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition;//这里直接返回第二层元素下标 } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.expandable_group, null); TextView text = (TextView)view.findViewById(R.id.text_group); text.setText(groups[groupPosition]); return view;//实际项目中应该考虑convertView的重用 } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.expandable_child, null); TextView text = (TextView)view.findViewById(R.id.text_child); text.setText(childs[groupPosition][childPosition]); return view; } @Override//组合子元素是否拥有稳定的id,底层的数据集改表是否影响该id public boolean hasStableIds() { return false; } @Override//子元素是否可选中,有点击反馈 public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
ExpandableListActivity和ListActivity类似
写自己的Activity继承自该类
该类自带一个ExpandableListView。id为:“@android:id/list”(或者代码中为“list”)
可以有一个id为:“@android:id/empty”布局,作为adapter没数据时的替代布局
通常写自己的布局并满足上述条件,并加以扩展实现自己的需求
SimpleCursorTreeAdapter
不同于SimpleExpandableListAdapter数据源来自List<Map<?,?>>,SimpleCursorTreeAdapter数据源来自访问数据库返回的Cursor。
本文出自 “一剑围城” 博客,请务必保留此出处http://weijiancheng.blog.51cto.com/10190955/1913815
标签:总结 expandablelistview expandablelistadapter expandablelistactivity
原文地址:http://weijiancheng.blog.51cto.com/10190955/1913815