使用ListView進行不同佈局ItemView的顯示
阿新 • • 發佈:2019-02-19
首先要說一下listview的繪製原理,在listview進行繪製的時候,首先要呼叫getCount()方法來確定listview的item個數,然後在繪製每個item的時候呼叫getView方法來進行繪製,而getItem和getItemId是在listview響應使用者操作時間時候進行呼叫的。/** * 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; } }