要想在一个ListView中使用多个布局文件,比如一个信息List包含了一个信息标题和每个信息对应的时间.
关键的步骤是实现Adapter类的getItemViewType 和getViewTypeCount 这两个方法
getItemViewType(int)
以int数值型返回itemView的类型。一般普通列表的item都是一样的布局,也就是说这个列表只有一种类型,但是很多时候我们需要列表显示不同的item,比如有的列表有普通item和separator两种类型,item用于响应用户点击事件,separator用于分隔item,不可以点击,这样这个列表就有了两种类型,重载这个方法,如果当前位置是item,我们可以返回1,如果是separator我们可以返回2,以此类推。重写getItemViewType方法,这个方法返回0到getViewTypeCount()-1之间的数字或者IGNORE_ITEM
getViewTypeCount()
返回这个Adapter将处理的itemView类型的总个数
1.首先定义一个接口,List中每一个Item项都必须实现它里面的getViewType和getView方法分别表示使用何种类型的View显示,以及如何新建和回收。
public interface Item {
public int getViewType();
public View getView(View convertView);
}
2.在Adapter中维护一个实现Item接口的List对象
public class MyListAdapter extends ArrayAdapter<Item> {
private List<Item> items;
private LayoutInflater inflater;
public enum RowType {
// Here we have two items types, you can have as many as you like though
LIST_ITEM, HEADER_ITEM
}
public MyListAdapter(Context context, LayoutInflater inflater, List<Item> items) {
super(context, 0, items);
this.items = items;
this.inflater = inflater;
}
@Override
public int getViewTypeCount() {
// Get the number of items in the enum
return RowType.values().length;
}
@Override
public int getItemViewType(int position) {
// Use getViewType from the Item interface
return items.get(position).getViewType();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Use getView from the Item interface
return items.get(position).getView(inflater, convertView);
}
}
3.现在创建一个标题类实现Item接口
public class Header implements Item {
private final String name;
public Header(String name) {
this.name = name;
}
@Override
public int getViewType() {
return RowType.HEADER_ITEM.ordinal();
}
@Override
public View getView(LayoutInflater inflater, View convertView) {
if (convertView == null) {
// No views to reuse, need to inflate a new view
convertView = (View) inflater.inflate(R.layout.header, null);
}
TextView text = (TextView) convertView.findViewById(R.id.headerText);
text.setText(name);
return convertView;
}
}
4.创建一个信息类实现Item接口
public class EventItem implements Item {
private final String str1;
private final String str2;
public EventItem(String text1, String text2) {
this.str1 = text1;
this.str2 = text2;
}
@Override
public int getViewType() {
return RowType.LIST_ITEM.ordinal();
}
@Override
public View getView(LayoutInflater inflater, View convertView) {
if (convertView == null) {
convertView = (View) inflater.inflate(R.layout.list_item, null);
}
TextView text1 = (TextView) convertView.findViewById(R.id.list_content1);
TextView text2 = (TextView) convertView.findViewById(R.id.list_content2);
text1.setText(str1);
text2.setText(str2);
return convertView;
}
}
5.最后是Activity的实现
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater inflater = LayoutInflater.from(this);
List<Item> items = new ArrayList<Item>();
items.add(new Header("Friday - November 30th 2012"));
items.add(new EventItem("8:30am" , "Start work"));
items.add(new EventItem("9:15am" , "Call Bob"));
items.add(new EventItem("11:00am", "Meeting with Joe"));
items.add(new EventItem("5:00pm" , "Freedom!"));
items.add(new Header("Saturday - December 1st 2012"));
items.add(new EventItem("8:30am" , "Keep sleeping"));
items.add(new EventItem("10:00am", "Wake up"));
items.add(new EventItem("11:00am", "Walk the dog"));
items.add(new EventItem("6:00pm" , "Dinner at John's"));
items.add(new Header("Sunday - December 2rd 2012"));
items.add(new EventItem("8:30am" , "Keep sleeping"));
items.add(new EventItem("10:00am", "Wake up"));
items.add(new EventItem("11:00am", "Walk the dog"));
items.add(new EventItem("6:00pm" , "Dinner at John's"));
items.add(new Header("Monday - December 3rd 2012"));
items.add(new EventItem("8:30am" , "Start work"));
items.add(new EventItem("9:15am" , "Call Bob"));
items.add(new EventItem("11:00am", "Meeting with Joe"));
items.add(new EventItem("5:00pm" , "Freedom!"));
MyListAdapter adapter = new MyListAdapter(this, inflater, items);
setListAdapter(adapter);
}
}