Android--购物车的实现详解

         现在做商城的越来越多,或者说添加商城功能的应用越来越多,昨天有人给我要购物车的Demo,所以本篇文章主要总结一下商城中购物车部分的实现过程和具体代码,整体实现虽然不难,但要注意它的逻辑关系,我使用的是本地数据,界面不是那么美观,大家就将就看一下主要的实现方式。

先看一下Activity的布局(我这使用的是Listview,写项目的话建议大家使用Recyclerview):




    
布局没什么好说的之后是Listview中item的布局:



    

    
        
        
        
        
需要为listview添加adapter,下边是我项目的adapter写法可能与平常你们的写法不同,只是个人习惯而已,在adapter中我做了 注释,可以根据注释理解。

public class ShopCarAdapter extends BaseAdapter{
    private Context mContext;
    private ListmInfos;
    private boolean mMode;
    private View.OnClickListener mOnClickListener;
    private CompoundButton.OnCheckedChangeListener mChangeListener;
    public ShopCarAdapter (Listinfos,Context context){
        mContext=context;
        mInfos=infos;
        mChangeListener= (CompoundButton.OnCheckedChangeListener) mContext;
        mOnClickListener= (View.OnClickListener) mContext;
    }

    @Override
    public int getCount() {
        return mInfos==null?0:mInfos.size();
    }

    @Override
    public Object getItem(int i) {
        return mInfos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null) {
            view= LayoutInflater.from(mContext).inflate(R.layout.item_shopcar,null);
            holder=new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }
        holder.mTextViewInfo.setText(mInfos.get(i).info);
        holder.mTextViewPrice.setText(mInfos.get(i).price+"");
        holder.mTextViewCount.setText(mInfos.get(i).count+"");
        //设置删除按钮的可见和不可见
        if(mMode) {
            holder.mButtonDelete.setVisibility(View.VISIBLE);
            holder.mButtonDelete.setEnabled(true);
        }else {
            holder.mButtonDelete.setVisibility(View.GONE);
        }
        //给CheckBox设置选中的监听事件
        holder.mCheckBox.setOnCheckedChangeListener(mChangeListener);
        holder.mCheckBox.setTag(i);
        //设置CheckBox的选中状态
        holder.mCheckBox.setChecked(mInfos.get(i).isCheck);

        //给添加和减少按钮设置点击事件
        holder.mButtonAdd.setOnClickListener(mOnClickListener);
        holder.mButtonSub.setOnClickListener(mOnClickListener);
        //关联button位置
        holder.mButtonAdd.setTag(i);
        holder.mButtonSub.setTag(i);
        //给删除按钮设置点击事件
        holder.mButtonDelete.setOnClickListener(mOnClickListener);
        holder.mButtonDelete.setTag(i);
        Picasso.with(mContext).load(mInfos.get(i).icon).into(holder.mImageViewIcon);
        return view;
    }

    public static class ViewHolder{
        private TextView mTextViewInfo,mTextViewCount,mTextViewPrice;
        private ImageView mImageViewIcon;
        private CheckBox mCheckBox;
        private Button mButtonAdd,mButtonSub,mButtonDelete;
        public ViewHolder(View itemView){
            mTextViewInfo= (TextView) itemView.findViewById(R.id.tv_item_info );
            mTextViewCount= (TextView) itemView.findViewById(R.id.tv_item_count);
            mTextViewPrice= (TextView) itemView.findViewById(R.id.tv_item_price);
            mImageViewIcon= (ImageView) itemView.findViewById(R.id.iv_item_icon);
            mCheckBox= (CheckBox) itemView.findViewById(R.id.cb_item_check);
            mButtonAdd= (Button) itemView.findViewById(R.id.bt_item_add);
            mButtonSub= (Button) itemView.findViewById(R.id.bt_item_subtract);
            mButtonDelete= (Button) itemView.findViewById(R.id.bt_item_delete);
        }
    }
    //以下两个方法会在Activity中调用
    public void setModel(boolean mode){
        mMode=mode;
        notifyDataSetChanged();
    }
    public int getMoney(){
        int money=0;
        if(mInfos==null||mInfos.size()==0){
            return money;
        }
        for (int i = 0; i 
最后就是Activity的部分了,我之所以把Activity放在最后了是因为我把Listview的item中的控件的点击事件在Adapter中做了关联,而监听的实现过程放在了Activity中更方便处理数据。

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private ListView mListView;
    private ListmData;
    private CheckBox mCheckBox;
    private Button mButtonMode;
    private TextView mTextViewMoney;
    private ShopCarAdapter mAdapter;
    private boolean mMode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mCheckBox.setOnCheckedChangeListener(this);
        mButtonMode.setOnClickListener(this);
    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void initData() {
        //初始化数据 自己模拟的数据
        mData=new ArrayList<>();
        for (int i = 0; i <30 ; i++) {
            ShopInfo info=new ShopInfo();
            info.count=i+1;
            info.price=(i+1)*2;
            info.icon="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1265160970,3356031848&fm=23&gp=0.jpg";
            if(i%3==0) {
                info.info="亚马逊";
            }else if(i%3==1) {
                info.info="京东";
            }else{
                info.info="天猫";
            }
            mData.add(info);
        }
        // 初始化adapter
         mAdapter=new ShopCarAdapter(mData,this);
        //给adapter设置一个数据改变的监听
        mAdapter.registerDataSetObserver(new DataSetObserver() {
            //当调用adapter的notofiedDataChange的时候就会调用这个方法
            @Override
            public void onChanged() {
                super.onChanged();
                int money=mAdapter.getMoney();
                mTextViewMoney.setText(money+"");
            }
        });

    }

    private void initView() {
        getSupportActionBar().hide();
        mListView= (ListView) findViewById(R.id.lv_main_list);
        mCheckBox= (CheckBox) findViewById(R.id.cb_main_checkAll);
        mButtonMode= (Button) findViewById(R.id.bt_main_mode);
        mTextViewMoney= (TextView) findViewById(R.id.tv_main_count);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch(buttonView.getId()){
            case R.id.cb_main_checkAll:
                for (int i = 0; i 
购物车的基本逻辑就完成了,下边看一下最终的实现结果:

    Android--购物车的实现详解_第1张图片


       源码地址:http://download.csdn.net/detail/huohao_blogs/9858505









你可能感兴趣的:(Android开发)