转载请标明出处:http://www.cnblogs.com/tanlon/archive/2011/06/20/2085583.html
通过前面三篇文章:
Android 适配器Adapter的学习:http://www.cnblogs.com/tanlon/archive/2011/05/21/2053009.html
Android 适配器Adpter的使用总结:http://www.cnblogs.com/tanlon/archive/2011/06/20/2084901.html
Android 适配器Adpter的使用总结 之 CursorAdpter:http://www.cnblogs.com/tanlon/archive/2011/06/20/2085562.html
我们对Adpter有了较深的理解之后,我们来了解一下BaseExpandableListAdapter也就是二级树ExpandableListView的数据填充器。
我们应该先了解一下关于BaseExpandableListAdapter中的一些方法的含义和用法:
http://www.cnblogs.com/over140/archive/2011/01/18/1937921.html
如我的上篇博文<Android 适配器Adpter的使用总结 之 CursorAdpter>提到的BaseExpandableListAdapter则是实现了ExpandableListAdpter、HeterogeneousExpandableList接口,同理CursorTreeAdapter继承了BaseExpandableListAdapter,并且衍生出了ResourceCusorTreeAdapter,由ResourceCusorTreeAdapter又衍生出了SimpleCursorTreeAdapter。除此之外还有SimpleExpandableListAdapter也是继承自BaseExpandableListAdapter.
在这里还是以SimpleExpandableListAdapter为例,做一个小demo:
布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:id="@+id/expandableListView"/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:id="@+id/expandablelistview_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="expandablelistview_tv"
android:layout_centerVertical="true"
android:paddingLeft="50dp"
android:textSize="24dp"
/>
</RelativeLayout>
数据填充器:
private Context context;
private List<Map<String, String>> groupData =new ArrayList<Map<String, String>>();//装载group数据
private List<List<Map<String, String>>> childData =new ArrayList<List<Map<String, String>>>();//装载child数据
privateint expandedGroupLayout;
privateint collapsedGroupLayout;
private String[] groupFrom;
privateint[] groupTo;
privateint childLayout;
privateint lastChildLayout;
private String[] childFrom;
privateint[] childTo;
//构造器
public ListSimpleExpandableListAdapter(Context context,
List<Map<String, String>> groupData, int expandedGroupLayout,
int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
List<List<Map<String, String>>> childData,
int childLayout, int lastChildLayout, String[] childFrom,
int[] childTo) {
super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom,
groupTo, childData, childLayout, lastChildLayout, childFrom, childTo);
// TODO Auto-generated constructor stub
this.context=context;
this.groupData=groupData;
this.childData=childData;
this.expandedGroupLayout=expandedGroupLayout;
this.collapsedGroupLayout=collapsedGroupLayout;
this.groupFrom=groupFrom;
this.groupTo=groupTo;
this.childLayout=childLayout;
this.lastChildLayout=lastChildLayout;
this.childFrom=childFrom;
this.childTo=childTo;
}
//获取group对象
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupData;
}
//获取group对象的数量
@Override
publicint getGroupCount() {
// TODO Auto-generated method stub
return groupData.size();
}
//获取group的id
@Override
publiclong getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GroupHolder groupHolder;
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(expandedGroupLayout, null);
groupHolder=new GroupHolder();
// groupHolder.imageView=(ImageView) convertView.findViewById(R.id.expandablelistview_iv);
groupHolder.textView=(TextView) convertView.findViewById(R.id.expandablelistview_tv);
convertView.setTag(groupHolder);
}else{
groupHolder=(GroupHolder) convertView.getTag();
}
// groupHolder.imageView.setImageResource(Integer.parseInt(groupData.get(groupPosition).get(groupFrom[0]).toString()));
groupHolder.textView.setText(groupData.get(groupPosition).get(groupFrom[1]).toString());
return convertView;
}
//子节点是否被选中
@Override
publicboolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
returntrue;
}
//获取子节点的数量
@Override
publicint getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childData.get(groupPosition).size();
}
//获取子节点对象
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childData.get(groupPosition).get(childPosition);
}
//获取子节点id
@Override
publiclong getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ChildHolder childHolder;
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(childLayout, null);
childHolder=new ChildHolder();
childHolder.imageView=(ImageView) convertView.findViewById(R.id.listitem_pic);
childHolder.title=(TextView) convertView.findViewById(R.id.listitem_title);
childHolder.content=(TextView) convertView.findViewById(R.id.listitem_content);
convertView.setTag(childHolder);
}else{
childHolder=(ChildHolder) convertView.getTag();
}
childHolder.imageView.setImageResource(Integer.parseInt(childData.get(groupPosition).get(childPosition).get(childFrom[0]).toString()));
childHolder.title.setText(childData.get(groupPosition).get(childPosition).get(childFrom[1]).toString());
childHolder.content.setText(childData.get(groupPosition).get(childPosition).get(childFrom[2]).toString());
return convertView;
}
class GroupHolder{
ImageView imageView;
TextView textView;
}
class ChildHolder{
ImageView imageView;
TextView title;
TextView content;
}
}
privatestaticfinal String ICON="ICON";
privatestaticfinal String NAME ="NAME";
privatestaticfinal String IS_EVEN ="IS_EVEN";
private ExpandableListAdapter mAdapter;
private ExpandableListView expandableListView;
protectedvoid onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.groupexpandablelist);
List<Map<String, String>> groupData =new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData =new ArrayList<List<Map<String, String>>>();
for (int i =0; i <20; i++) {
Map<String, String> curGroupMap =new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(ICON, String.valueOf(R.drawable.icon));
curGroupMap.put(NAME, "Group "+ i);
curGroupMap.put(IS_EVEN, (i %2==0) ?"This group is even" : "This group is odd");
List<Map<String, String>> children =new ArrayList<Map<String, String>>();
for (int j =0; j <15; j++) {
Map<String, String> curChildMap =new HashMap<String, String>();
curChildMap.put(ICON, String.valueOf(R.drawable.icon));
curChildMap.put(NAME, "Child "+ j);
curChildMap.put(IS_EVEN, (j %2==0) ?"This child is even" : "This child is odd");
children.add(curChildMap);
}
childData.add(children);
}
expandableListView=(ExpandableListView) findViewById(R.id.expandableListView);
mAdapter=new ListSimpleExpandableListAdapter(this, groupData, R.layout.groupexpandablelist_item, 0, new String[] { ICON ,NAME, IS_EVEN }, null, childData, R.layout.listsimple_item, 0, new String[] { ICON ,NAME, IS_EVEN }, null);
expandableListView.setAdapter(mAdapter);
//传入null则是消除箭头,如果想替换成自己的图标则传入一个drawable即可
// this.expandableListView.setGroupIndicator(null);
}
}
有什么不对的地方还望大牛多多指教!
相关代码发布在QQ群:Android中高级技术免费培训QQ群(118949422)
很晚了,睡觉去了,明天有机会再写。