Android listView奇偶項採用不同的佈局

yangxi_001發表於2014-08-15
public class TimelineAdapter extends BaseAdapter {

private List<Map<String, Object>> list;
private LayoutInflater inflater;
public TimelineAdapter(List<Map<String, Object>> list) {
    super();
    this.list = list;
}
@Override
public int getCount() {
    return list.size();
}
@Override
public Object getItem(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    View convertView_odd = null, convertView_even = null;
    if(position%2 == 0){
       if (convertView_even == null) {
           inflater = LayoutInflater.from(parent.getContext());
           convertView_even = inflater.inflate(R.layout.listview_item_even, null);
           viewHolder = new ViewHolder();
           viewHolder.title = (TextView) convertView_even.findViewById(R.id.title);
           convertView_even.setTag(viewHolder);
       } 
       convertView = convertView_even;
    } else{
       if (convertView_odd == null) {
           inflater = LayoutInflater.from(parent.getContext());
           convertView_odd = inflater.inflate(R.layout.listview_item_odd, null);
           viewHolder = new ViewHolder();
           viewHolder.title = (TextView) convertView_odd.findViewById(R.id.title);
           convertView_odd.setTag(viewHolder);
       } 
       convertView = convertView_odd;
    }
    String titleStr = list.get(position).get("title").toString();
    viewHolder.title.setText(titleStr);

    return convertView;
}
    static class ViewHolder {
        public TextView title;
    }
}

相關文章