主要是使用分页显示商品 创建一个适配器, (1)通过item的position,对item布局的显示, 第一个item即0, 显头标题; (2)显示商品 (3)最后一个Item显示更多, 用来加载数据 (4)通过checkbox的选中状态,对商品进行管理, 删除
/** * 自定义Adapter * * @author jhao * @version 1.0 */ class MyAdapter extends BaseAdapter { private Context context; private int res_item_layout = R.layout.productitem; public MyAdapter(Context context) { this.context = context; } public MyAdapter(Context context, int res_item_layout) { this.context = context; this.res_item_layout = res_item_layout; init(); } /** * 初始化内部的用于保存checkbox状态的集合 */ private void init() { isSelected = new HashMap<Integer, Boolean>(); for (int i = 0; i < products.size(); i++) { isSelected.put(i, false); } } /** * 获取更多(分页获取) */ public void more() { // step 1: 当前页++ pageIndex++; // step 2:获取更多的数据 List<Product> moreProducts = productManager.getProdcutByPager( pageIndex, PAGESIZE); if (moreProducts != null) { // step 3:添加到现有的集合中 products.addAll(moreProducts); // 刷新ListView this.notifyDataSetChanged(); } else { Tool.ShowMessage(context, "已经是最后一条记录了!"); } } /** * 刷新ListView */ public void refresh() { if (this.isBatchManager()) init(); this.notifyDataSetChanged(); } /** * 用于判断是否是处于批量管理状态,true:是,false:否 * * @return */ public boolean isBatchManager() { return this.res_item_layout == R.layout.productitem2; } @Override public int getCount() { return products.size() + 2; } @Override public Object getItem(int position) { return products.get(position - 1); } @Override public long getItemId(int position) { // return products.get(position - 1).getId(); if (position == 0)// 选中第一项 { return -1;// 代表点击的是第一项 } else if (position > 0 && (position < this.getCount() - 1)) { return products.get(position - 1).getId();// 如果用户选中了中间项 // mUserList.get(index-1).getId(); } else { return -2;// 表示用户选中最后一项 } } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView img = null; TextView title = null; TextView unitPrice = null; // 说明是第一项 if (position == 0) { convertView = inflater.inflate(R.layout.addproductitem, null); ImageView imgAdd = (ImageView) convertView .findViewById(R.id.imgAdd); imgAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Tool.ShowMessage(context, "添加!"); } }); return convertView; } // 说明是最后一项 if (position == this.getCount() - 1) { convertView = inflater.inflate(R.layout.moreitemsview, null); return convertView; } ViewHolder holder = null; if (convertView == null || convertView.findViewById(R.id.addproduct) != null || convertView.findViewById(R.id.linemore) != null) { /* * convertView = inflater.inflate(R.layout.productitem, parent, * false); */ holder = new ViewHolder(); convertView = inflater.inflate(this.res_item_layout, parent, false); holder.img = (ImageView) convertView .findViewById(R.id.imgPhoto); holder.title = (TextView) convertView .findViewById(R.id.txtName); holder.unitPrice = (TextView) convertView .findViewById(R.id.txtUnitPrice); holder.cBox = (CheckBox) convertView.findViewById(R.id.chkIn); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (holder.cBox != null) holder.cBox.setChecked(isSelected.get(position - 1)); holder.img.setImageResource(products.get(position - 1).getPhoto()); holder.title.setText(position + "、" + products.get(position - 1).getName().toString()); holder.unitPrice.setText(String.valueOf(products.get(position - 1) .getUnitPrice())); return convertView; } } public final class ViewHolder { public ImageView img; public TextView title; public TextView unitPrice; public CheckBox cBox; }