最近开始学安卓,ExpandableList部分,于是想着在child前面加个图片试试,参考网上一些代码,总算是弄出来了,记录如下:
先上效果图,布局还有图片资源什么的太费劲就没去弄了。
首先是在Activity中相关list数据的构造,这里需要3个list,
1.group的list
2.group下child的list
3.整个child构成的list
package com.example.expandablelist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.R.string;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
public class MainActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建group list对象,这里总共有2个group
List<Map<String, String>> groups = new ArrayList<Map<String,String>>();
Map<String, String> group1 = new HashMap<String, String>();
Map<String, String> group2 = new HashMap<String, String>();
group1.put("group", "group1");
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);
//创建group下的child对象,这个是放在group1下面的第一个childlist,因为要调用图片
//所以map的value使用的是Integer类型,去调用resource里面定义的数据。
List<Map<String, Integer>> child1 = new ArrayList<Map<String,Integer>>();
Map<String, Integer> child1data1 = new HashMap<String, Integer>();
Map<String, Integer> child1data2 = new HashMap<String, Integer>();
Map<String, Integer> child1data3 = new HashMap<String, Integer>();
//放入数据,数据的赋值可以另建一函数,这里图片直接调用系统
child1data1.put("child", R.string.child1data1);
child1data1.put("name", R.string.child1name1);
child1data2.put("child", R.string.child1data2);
child1data2.put("name", R.string.child1name2);
child1data1.put("image", R.drawable.ic_launcher);
child1data2.put("image", R.drawable.ic_launcher);
child1data3.put("child", R.string.child1data3);
child1data3.put("name", R.string.child1name3);
child1data3.put("image", R.drawable.ic_launcher);
child1.add(child1data1);
child1.add(child1data2);
child1.add(child1data3);
//第二个childlist,这里我把数据屏蔽了。
List<Map<String, Integer>> child2 = new ArrayList<Map<String,Integer>>();
Map<String, Integer> child2data1 = new HashMap<String, Integer>();
//child2data1.put("child", "child2data1");
//child2data1.put("name", "wangwu");
//child2.add(child2data1);
//创建一个总的childs list对象,将上面的child放入到childs list中
List<List<Map<String, Integer>>> childs = new ArrayList<List<Map<String,Integer>>>();
childs.add(child1);
childs.add(child2);
//根据自定义的adapter创建一个对象,并设置adpater。
ImageAdapter expandableListAdapter = new ImageAdapter(this, groups, R.layout.group, new String [] {"group"}, new int []{R.id.groupTo}, childs, R.layout.child, new String []{"image","child","name"}, new int[]{R.id.childmap,R.id.childTo1,R.id.childTo2});
setListAdapter(expandableListAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
然后是自定义adapter的创建,这里主要是实现adpater中的getChildView方法,
package com.example.expandablelist;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class ImageAdapter extends SimpleExpandableListAdapter {
List<Map<String, String>> groupList = null;
List<List<Map<String, Integer>>> childList = null;
Activity activity;
//下面是一系列复用。
public ImageAdapter(Context context,
List<Map<String, String>> groupData, int expandedGroupLayout,
String[] groupFrom, int[] groupTo,
List<List<Map<String, Integer>>> childData,
int childLayout, String[] childFrom,
int[] childTo) {
super(context, groupData, expandedGroupLayout, groupFrom,
groupTo, childData, childLayout, childFrom, childTo);
this.groupList = groupData;
this.activity = (Activity) context;
// TODO Auto-generated constructor stub
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return super.getChild(groupPosition, childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return super.getChildId(groupPosition, childPosition);
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return super.getChildrenCount(groupPosition);
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return super.getGroup(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return super.getGroupCount();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return super.getGroupId(groupPosition);
}
private class ViewHolder {
ImageView image;
TextView child;
TextView name;
}
//
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null)
{
//获取布局文件,这个布局文件就是child显示的布局文件
convertView = inflater.inflate(R.layout.child, null);
holder = new ViewHolder();
//获取行对应的id
holder.image = (ImageView) convertView.findViewById(R.id.childmap);
holder.child = (TextView) convertView.findViewById(R.id.childTo1);
holder.name = (TextView) convertView.findViewById(R.id.childTo2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
//根据group和child的position来获取child对象
Map<String, Integer> temp = (Map<String, Integer>) getChild(groupPosition, childPosition);
//给对应的child对象赋值
holder.image.setImageResource(temp.get("image"));
holder.child.setText(temp.get("child"));
holder.name.setText(temp.get("name"));
return convertView;
}
}
通过实现getChildView方法,就可以在child里面显示图片。
以上是全部代码,水平有限,若有更好方法,欢迎指教!