我的淘宝简单实现

127 阅读3分钟

我的淘宝简单实现

       这里写图片描述

主要是使用分页显示商品
创建一个适配器,
(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;
    }

商品初始化:

    private Product[] products={
            new Product(1,"三星 GT-S5830", 1, R.drawable.p1,
                    1630, "网络类型:WCDMA(3G) 操作系统:ANDROID "),
            new Product(2,"HTC A510e", 1, R.drawable.p2,
                            1514, "网络类型:WCDMA(3G) 操作系统:ANDROID ")   ,   
            new Product(3,"三星 I9100", 1, R.drawable.p3,
                    3266, "网络类型:WCDMA(3G) 操作系统:ANDROID "),
            new Product(4,"中兴 U880(TD版)", 1,
                    R.drawable.p4, 989,
                            "网络类型:TD-SCDMA(3G) 操作系统:ANDROID  "),
            new Product(5,"Sony Ericsson索尼爱立信 ", 1,
                    R.drawable.p5, 2584,
                                    "网络类型:WCDMA(3G) 操作系统:ANDROID "),
            new Product(6,"摩托罗拉 Defy", 1, R.drawable.p6,
                1851, "网络类型:WCDMA(3G) 操作系统:ANDROID "),
                new Product(7,"中兴 V880", 1, R.drawable.p7,
                        957, "网络类型:WCDMA(3G) 操作系统:ANDROID "),
            new Product(8,"HTC S710e", 1, R.drawable.p8,
                                2671, "网络类型:WCDMA(3G) 操作系统:ANDROID "),
            new Product(9,"摩托罗拉 ME525",1, R.drawable.p9,
                                        1853, "网络类型:WCDMA(3G) 操作系统:ANDROID "),
            new Product(10,"G12", 1, R.drawable.p10,
                                                2470, "网络类型:WCDMA(3G) 操作系统:ANDROID "),
            new Product(11,"摩托罗拉 ME525+", 1,
                    R.drawable.p11, 1923,
                                                        "网络类型:WCDMA(3G) 操作系统:ANDROID "),    
            new Product(12,"Sony Ericsson", 1,
                    R.drawable.p12, 2231,"网络类型:WCDMA(3G) 操作系统:ANDROID "),
            new Product(13,"Lenovo", 1, R.drawable.p13,997, "网络类型:WCDMA(3G) 操作系统:ANDROID ")     
    };

本例中最重要的一个思想是通过接口, 和XXManager将一些列操作进行封装, 进行了松耦合