最近android开发接触ListView比较多,可以通过重写Adapter适配器简单实现一些效果:例如分组,类似通讯录中分组功能;在Item中放图片,按钮等其他组合控件等。
思路如下:重写BaseAdapter类,其中getView函数中判断是否分组,重新定义每一个item中view的内容。
代码如下:
public class MyPOIAdapter extends BaseAdapter {
private List<Map<String, Object>> m_listData;
//private List<String> m_listTag;
private LayoutInflater m_Inflater;
private String m_currentType = "";
public MyPOIAdapter(Context context, List<Map<String, Object>> listData)
{
m_Inflater = LayoutInflater.from(context);
m_listData = listData;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return m_listData.size();
}
@Override
public Object getItem(int pos) {
// TODO Auto-generated method stub
return m_listData.get(pos);
}
@Override
public long getItemId(int pos) {
// TODO Auto-generated method stub
return pos;
}
@SuppressWarnings("unchecked")
@Override
/*classify POIs, and set POI content, including name and images and some information
* */
public View getView(final int pos, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Map<String, Object> mapPoiInfo = null;
mapPoiInfo = (Map<String, Object>)getItem(pos);
if(mapPoiInfo == null)
return null;
//type name tag
if(IsTag)
{
convertView = m_Inflater.inflate(R.layout.list_items_tag_poi, null);
TextView textView = (TextView)convertView.findViewById(R.id.POI_tagName);
textView.setText((String)mapPoiInfo.get("POIType"));
}
else
{
convertView = m_Inflater.inflate(R.layout.list_items_poi, null);
ImageView imgView = (ImageView)convertView.findViewById(R.id.POI_img);
TextView textViewName = (TextView)convertView.findViewById(R.id.POI_name);
TextView textViewInfo = (TextView)convertView.findViewById(R.id.POI_info);
Button btnPOIInfo = (Button)convertView.findViewById(R.id.POI_info_btn);
//按钮事件
btnPOIInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
return convertView;
}
上述简单实现了如果是组名时,调用类别的xml布局文件,否则调用另一个xml布局文件。
另外还发现一个BaseExpandableListAdapter这个类可以更好地实现分组的效果,还可以收缩等。所以,,又要改代码了