1.首先是两个数据模型类
package com.example.titlelistviewdemo;
public class Data {
private String text1, text2, text3; //数据1 2 3
public Data(String text1, String text2, String text3) {
this.text1 = text1;
this.text2 = text2;
this.text3 = text3;
}
public String getText1() {
return text1;
}
public String getText2() {
return text2;
}
public String getText3() {
return text3;
}
}
package com.example.titlelistviewdemo;
import java.util.ArrayList;
import java.util.List;
public class Type {
private String title; //ListView头部显示的标题
private List<Data> mList; //头部对应的内容集合
public Type(String title) {
this.title = title;
mList = new ArrayList<Data>();
}
/**
* 添加项目
* @param data Data对象
*/
public void addItem(Data data) {
mList.add(data);
}
/**
* 获取项目
* @param position 如果position为1就返回标题
* @return
*/
public Object getItem(int position) {
if (position == 0) {
return title;
} else {
return mList.get(position - 1);
}
}
/**
* @return item数目,为集合大小+1
*/
public int size() {
return mList.size() + 1;
}
}
2.自己写的adapter
package com.example.titlelistviewdemo;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private static final int TYPE_HEADER = 0; //代表标题
private static final int TYPE_ITEM = 1; //代表项目item
private List<Type> mList;
private LayoutIn