/** * Created by Kent on 2014/12/12. */ public class MyAdapter extends BaseAdapter { private Context mContext = null;//上下文 private LayoutInflater mInflater = null; private List<BaseItem> mData = null;//要显示的数据 public MyAdapter(Context context, List<BaseItem> data){ this.mInflater = LayoutInflater.from(context); this.mData = data; } //添加一个新的Item,并通知listview进行显示刷新 public void addItem(BaseItem newItem){ this.mData.add(newItem); this.notifyDataSetChanged(); } @Override public int getItemViewType(int position) { return mData.get(position).getItem_type(); } @Override public int getViewTypeCount() { return ItemType.ITEM_TYPE_MAX_COUNT; } @Override public int getCount() { if(mData == null){ return 0; } return this.mData.size(); } @Override public Object getItem(int i) { return mData.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int position, View convertView, ViewGroup viewGroup) { View viewItem1 = null; View viewItem2 = null; int itemType = this.getItemViewType(position); if(itemType == ViewHolder1.ITEM_VIEW_TYPE_1){ //第一种item ViewHolder1 viewHolder1 = null; if(convertView == null){ //没有缓存过 viewHolder1 = new ViewHolder1(); viewItem1 = this.mInflater.inflate(R.layout.list_view_item_1, null, false); viewHolder1.textView = (TextView)viewItem1.findViewById(R.id. main_activity_list_view_item_1_textview); viewHolder1.imageView = (ImageView)viewItem1.findViewById(R.id. main_activity_list_view_item_1_imageview); viewItem1.setTag(viewHolder1); convertView = viewItem1; }else{ viewHolder1 = (ViewHolder1)convertView.getTag(); } viewHolder1.textView.setText(((ItemBean1) mData.get(position)).getName()); viewHolder1.imageView.setBackgroundResource(R.drawable.ic_launcher); }else if(itemType == ViewHolder2.ITEM_VIEW_TYPE_2){ //第二种item ViewHolder2 viewHolder2 = null; if(convertView == null){ //没有缓存过 viewHolder2 = new ViewHolder2(); viewItem2 = this.mInflater.inflate(R.layout.list_view_item_2, null, false); viewHolder2.textView1 = (TextView)viewItem2.findViewById(R.id. main_activity_list_view_item_2_textview); viewHolder2.textView2 = (TextView)viewItem2.findViewById(R.id. main_activity_list_view_item_2_textview_2); viewItem2.setTag(viewHolder2); convertView = viewItem2; }else{ viewHolder2 = (ViewHolder2)convertView.getTag(); } viewHolder2.textView1.setText(((ItemBean2)mData.get(position)).getName()); viewHolder2.textView2.setText(((ItemBean2)mData.get(position)).getAddress()); } return convertView; } }首先要说一下listview的绘制原理,在listview进行绘制的时候,首先要调用getCount()方法来确定listview的item个数,然后在绘制每个item的时候调用getView方法来进行绘制,而getItem和getItemId是在listview响应用户操作时间时候进行调用的。
/** * Created by Kent on 2014/12/15. */ public class BaseItem { private int item_type = 0; public BaseItem(int item_type) { this.item_type = item_type; } public int getItem_type() { return item_type; } public void setItem_type(int item_type) { this.item_type = item_type; } }由于每个Item都需要一个type类型的变量来标识当前Item的种类,所以定义了一个基类:BaseItem类,它只包含一个item_type的属性,用来标识item的种类。
/** * Created by Kent on 2014/12/15. */ public class ItemBean1 extends BaseItem{ private String name = null; private String imagePath = null; public ItemBean1(int item_type, String name, String imagePath) { super(item_type); this.name = name; this.imagePath = imagePath; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } public int getItemType(){ return super.getItem_type(); } public void setItemType(int itemType){ super.setItem_type(itemType); } }4. MainActivity中的代码:
public class MainActivity extends Activity { private ListView listView = null; //适配器 private MyAdapter myAdapter = null; //数据 private List<BaseItem> mData = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewsById();; init(); addListeners(); } private void findViewsById(){ this.listView = (ListView)findViewById(R.id.main_activity_listview); } private void init(){ this.mData = new ArrayList<BaseItem>(); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name1", "iamgePath1")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean1(ViewHolder1.ITEM_VIEW_TYPE_1, "name2", "iamgePath2")); this.mData.add(new ItemBean2(ViewHolder2.ITEM_VIEW_TYPE_2, "name1", "address1")); this.myAdapter = new MyAdapter(this, this.mData); this.listView.setAdapter(this.myAdapter); } private void addListeners(){ } }5. ViewHolder,用于ItemView的缓存优化:
/** * Created by Kent on 2014/12/12. */ public final class ViewHolder1 { public static final int ITEM_VIEW_TYPE_1 = 0; public TextView textView = null; public ImageView imageView = null; }
/** * Created by Kent on 2014/12/12. */ public final class ViewHolder2 { public static final int ITEM_VIEW_TYPE_2 = 1; public TextView textView1 = null; public TextView textView2 = null; }
/** * Created by Kent on 2014/12/15. */ public class ItemType { public static final int ITEM_TYPE_MAX_COUNT = 2; }6. 实现不同item类型的显示;
public int getItemViewType(int position) { return mData.get(position).getItem_type(); }这个方法返回了当前要绘制的ItemView的类型,类型数据存储在listView的数据源List中的每个bean的itemType属性中。
public int getViewTypeCount() { return ItemType.ITEM_TYPE_MAX_COUNT; }这个方法返回了不同Item的种类个数。
public View getView(int position, View convertView, ViewGroup viewGroup) { View viewItem1 = null; View viewItem2 = null; int itemType = this.getItemViewType(position); if(itemType == ViewHolder1.ITEM_VIEW_TYPE_1){ //第一种item ViewHolder1 viewHolder1 = null; if(convertView == null){ //没有缓存过 viewHolder1 = new ViewHolder1(); viewItem1 = this.mInflater.inflate(R.layout.list_view_item_1, null, false); viewHolder1.textView = (TextView)viewItem1.findViewById(R.id. main_activity_list_view_item_1_textview); viewHolder1.imageView = (ImageView)viewItem1.findViewById(R.id. main_activity_list_view_item_1_imageview); viewItem1.setTag(viewHolder1); convertView = viewItem1; }else{ viewHolder1 = (ViewHolder1)convertView.getTag(); } viewHolder1.textView.setText(((ItemBean1) mData.get(position)).getName()); viewHolder1.imageView.setBackgroundResource(R.drawable.ic_launcher); }else if(itemType == ViewHolder2.ITEM_VIEW_TYPE_2){ //第二种item ViewHolder2 viewHolder2 = null; if(convertView == null){ //没有缓存过 viewHolder2 = new ViewHolder2(); viewItem2 = this.mInflater.inflate(R.layout.list_view_item_2, null, false); viewHolder2.textView1 = (TextView)viewItem2.findViewById(R.id. main_activity_list_view_item_2_textview); viewHolder2.textView2 = (TextView)viewItem2.findViewById(R.id. main_activity_list_view_item_2_textview_2); viewItem2.setTag(viewHolder2); convertView = viewItem2; }else{ viewHolder2 = (ViewHolder2)convertView.getTag(); } viewHolder2.textView1.setText(((ItemBean2)mData.get(position)).getName()); viewHolder2.textView2.setText(((ItemBean2)mData.get(position)).getAddress()); } return convertView; }前面已经说过了,getView是在每个Item进行绘制的时候进行调用的,那么想要进行不同类型Item的显示,在这个方法中根据item的类型进行不同的绘制就可以了。
int itemType = this.getItemViewType(position);获取到当前要绘制的Item的种类。然后根据不同的种类进行不同的布局导入。
viewItem1 = this.mInflater.inflate(R.layout.list_view_item_1, null, false);这个方法使用了ViewHolder进行itemView的缓存。有关内容在此不做解释
原文地址:http://blog.csdn.net/kent_todo/article/details/41943935