首页

android 仿淘宝购物车商品选择 源码下载

java

2020-8-21

package com.jock.tbshopcar;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jock.tbshopcar.adapter.MyExpandableListAdapter;
import com.jock.tbshopcar.dao.DBHelper;
import com.jock.tbshopcar.entity.ShoppingCartBean;
import com.jock.tbshopcar.listener.OnShoppingCartChangeListener;
import com.jock.tbshopcar.listener.ResponseCallBack;
import com.jock.tbshopcar.listener.ShoppingCartBiz;
import com.jock.tbshopcar.listener.ShoppingCartHttpBiz;
import com.jock.tbshopcar.utils.ToastHelper;


public class ShoppingCartActivity extends Activity {
    ExpandableListView expandableListView;
    ImageView ivSelectAll;
    TextView btnSettle;
    TextView tvCountMoney;
    TextView tvTitle;
    RelativeLayout rlShoppingCartEmpty;
    RelativeLayout rlBottomBar;

    private List<ShoppingCartBean> mListGoods = new ArrayList<ShoppingCartBean>();
    private MyExpandableListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 
        setContentView(R.layout.activity_shopping_cart);
        DBHelper.init(getApplicationContext());
        ToastHelper.getInstance().init(getApplicationContext());
        initView();
        setAdapter();
        requestShoppingCartList();
       
      
        
    }


    private void setAdapter() {
        adapter = new MyExpandableListAdapter(this);
        expandableListView.setAdapter(adapter);
        adapter.setOnShoppingCartChangeListener(new OnShoppingCartChangeListener() {
           
            public void onDataChange(String selectCount, String selectMoney) {
                int goodsCount = ShoppingCartBiz.getGoodsCount();
//                if (!isNetworkOk) {//网络状态判断暂时不显示
//                }
                if (goodsCount == 0) {
                    showEmpty(true);
                } else {
                    showEmpty(false);//其实不需要做这个判断,因为没有商品的时候,必须退出去添加商品;
                }
                String countMoney = String.format(getResources().getString(R.string.count_money), selectMoney);
                String countGoods = String.format(getResources().getString(R.string.count_goods), selectCount);
                String title = String.format(getResources().getString(R.string.shop_title), goodsCount   "");
                tvCountMoney.setText(countMoney);
                btnSettle.setText(countGoods);
                tvTitle.setText(title);
            }

            
            public void onSelectItem(boolean isSelectedAll) {
                ShoppingCartBiz.checkItem(isSelectedAll, ivSelectAll);
            }
        });
        //通过监听器关联Activity和Adapter的关系,解耦;
        View.OnClickListener listener = adapter.getAdapterListener();
        if (listener != null) {
            //即使换了一个新的Adapter,也要将“全选事件”传递给adapter处理;
            ivSelectAll.setOnClickListener(adapter.getAdapterListener());
            //结算时,一般是需要将数据传给订单界面的
            btnSettle.setOnClickListener(adapter.getAdapterListener());
        }
        expandableListView.setGroupIndicator(null);
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
           
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });
    }

    public void showEmpty(boolean isEmpty) {
        if (isEmpty) {
            expandableListView.setVisibility(View.GONE);
            rlShoppingCartEmpty.setVisibility(View.VISIBLE);
            rlBottomBar.setVisibility(View.GONE);
        } else {
            expandableListView.setVisibility(View.VISIBLE);
            rlShoppingCartEmpty.setVisibility(View.GONE);
            rlBottomBar.setVisibility(View.VISIBLE);
        }
    }

    private void initView() {
    	  expandableListView=       $(R.id.expandableListView);
          ivSelectAll=    $(R.id.ivSelectAll);
          btnSettle=   $(R.id.btnSettle);
          tvCountMoney=  $(R.id.tvCountMoney);
          tvTitle=   $(R.id.tvTitle);
          rlShoppingCartEmpty=    $(R.id.rlShoppingCartEmpty);
          rlBottomBar=    $(R.id.rlBottomBar);
     
    }

    /** 获取购物车列表的数据(数据和网络请求也是非通用部分) */
    private void requestShoppingCartList() {
        ShoppingCartBiz.delAllGoods();
        testAddGood();
        //使用本地JSON,作测试用。本来应该是将商品ID发送的服务器,服务器返回对应的商品信息;
        ShoppingCartHttpBiz.requestOrderList(this, new ResponseCallBack() {//requestOrderList(list, new VollyHelperNew.ResponseCallBack())
          
         

			public void handleResponse(Object o, int code)
			{
				// TODO Auto-generated method stub
				   mListGoods = ShoppingCartHttpBiz.handleOrderList((JSONObject)o, code);
	                ShoppingCartBiz.updateShopList(mListGoods);
	                updateListView();
			}
        });
    }

    private void updateListView() {
        adapter.setList(mListGoods);
        adapter.notifyDataSetChanged();
        expandAllGroup();
    }

    /**
     * 展开所有组
     */
    private void expandAllGroup() {
        for (int i = 0; i < mListGoods.size(); i  ) {
            expandableListView.expandGroup(i);
        }
    }

    /** 测试添加数据 ,添加的动作是通用的,但数据上只是添加ID而已,数据非通用 */
    private void testAddGood() {
        ShoppingCartBiz.addGoodToCart("279457f3-4692-43bf-9676-fa9ab9155c38", "6");
        ShoppingCartBiz.addGoodToCart("95fbe11d-7303-4b9f-8ca4-537d06ce2f8a", "8");
        ShoppingCartBiz.addGoodToCart("8c6e52fb-d57c-45ee-8f05-50905138801b", "9");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801d", "3");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801e", "3");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801f", "3");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801g", "3");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801h", "3");
    }
    
    /**
	 * 省去类型转换  将此方法写在基类Activity
	 */
	protected <T extends View> T $(int id)
	{
		return (T) super.findViewById(id);
	}

}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
TBShopCar/TBShopCar/.classpath , 533
TBShopCar/TBShopCar/.project , 845
TBShopCar/TBShopCar/AndroidManifest.xml , 869
TBShopCar/TBShopCar/assets/firm_order.json , 3495
TBShopCar/TBShopCar/bin/AndroidManifest.xml , 869
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/adapter/MyExpandableListAdapter$1.class , 3850
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/adapter/MyExpandableListAdapter$2.class , 1984
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/adapter/MyExpandableListAdapter$ChildViewHolder.class , 986
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/adapter/MyExpandableListAdapter$GroupViewHolder.class , 745
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/adapter/MyExpandableListAdapter.class , 9318
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/BuildConfig.class , 345
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/dao/DBHelper.class , 1721
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/dao/ShoppingCartDao.class , 4342
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/entity/ShoppingCartBean$Dispatch.class , 1486
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/entity/ShoppingCartBean$Goods.class , 3415
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/entity/ShoppingCartBean.class , 2960
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/listener/OnShoppingCartChangeListener.class , 260
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/listener/ResponseCallBack.class , 191
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/listener/ShoppingCartBiz.class , 6500
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/listener/ShoppingCartHttpBiz$1.class , 663
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/listener/ShoppingCartHttpBiz.class , 4546
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R$attr.class , 340
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R$color.class , 570
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R$dimen.class , 461
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R$drawable.class , 762
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R$id.class , 1542
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R$layout.class , 651
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R$string.class , 579
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R$style.class , 471
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/R.class , 675
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/ShoppingCartActivity$1.class , 2000
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/ShoppingCartActivity$2.class , 987
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/ShoppingCartActivity$3.class , 1275
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/ShoppingCartActivity.class , 5225
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/utils/DecimalUtil.class , 2209
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/utils/JsonReponseHandler.class , 2044
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/utils/ToastHelper$1$1.class , 1435
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/utils/ToastHelper$1.class , 1151
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/utils/ToastHelper.class , 2394
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/view/UIAlertView$clickListener.class , 1295
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/view/UIAlertView$ClickListenerInterface.class , 288
TBShopCar/TBShopCar/bin/classes/com/jock/tbshopcar/view/UIAlertView.class , 3611
TBShopCar/TBShopCar/bin/classes.dex , 1666276
TBShopCar/TBShopCar/bin/dexedLibs/android-support-v4-b493ac7bda88cd6168cd945a8088d301.jar , 472014
TBShopCar/TBShopCar/bin/dexedLibs/annotations-cd10ad8c83e2e68ee326c1a50de2107e.jar , 1265
TBShopCar/TBShopCar/bin/dexedLibs/gson-2.2.4-7cd0047497f4e78b09e871c7cdeb82bb.jar , 70809
TBShopCar/TBShopCar/bin/jarlist.cache , 120
TBShopCar/TBShopCar/bin/res/crunch/drawable-hdpi/ic_launcher.png , 9193
TBShopCar/TBShopCar/bin/res/crunch/drawable-mdpi/ic_launcher.png , 5057
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/ic_add.png , 476
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/ic_arrow.png , 768
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/ic_checked.png , 2950
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 14068
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/ic_reduce.png , 261
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/ic_shop.png , 2054
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/ic_shopping_cart_empty.png , 21200
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/ic_uncheck.png , 2634
TBShopCar/TBShopCar/bin/res/crunch/drawable-xhdpi/shopcar_select.png , 388
TBShopCar/TBShopCar/bin/resources.ap_ , 316069
TBShopCar/TBShopCar/bin/TBShopCar.apk , 869541
TBShopCar/TBShopCar/gen/com/jock/tbshopcar/BuildConfig.java , 160
TBShopCar/TBShopCar/gen/com/jock/tbshopcar/R.java , 5980
TBShopCar/TBShopCar/libs/android-support-v4.jar , 1422188
TBShopCar/TBShopCar/libs/gson-2.2.4.jar , 190418
TBShopCar/TBShopCar/proguard-project.txt , 781
TBShopCar/TBShopCar/project.properties , 563
TBShopCar/TBShopCar/res/drawable/shape_ui_alert_view.xml , 189
TBShopCar/TBShopCar/res/drawable-hdpi/ic_launcher.png , 9397
TBShopCar/TBShopCar/res/drawable-mdpi/ic_launcher.png , 5237
TBShopCar/TBShopCar/res/drawable-xhdpi/ic_add.png , 3078
TBShopCar/TBShopCar/res/drawable-xhdpi/ic_arrow.png , 18618
TBShopCar/TBShopCar/res/drawable-xhdpi/ic_checked.png , 5665
TBShopCar/TBShopCar/res/drawable-xhdpi/ic_launcher.png , 14383
TBShopCar/TBShopCar/res/drawable-xhdpi/ic_reduce.png , 2971
TBShopCar/TBShopCar/res/drawable-xhdpi/ic_shop.png , 20134
TBShopCar/TBShopCar/res/drawable-xhdpi/ic_shopping_cart_empty.png , 23845
TBShopCar/TBShopCar/res/drawable-xhdpi/ic_uncheck.png , 5634
TBShopCar/TBShopCar/res/drawable-xhdpi/shopcar_select.png , 1611
TBShopCar/TBShopCar/res/drawable-xhdpi/test_goods.jpg , 241487
TBShopCar/TBShopCar/res/layout/activity_shopping_cart.xml , 5041
TBShopCar/TBShopCar/res/layout/content_expandable.xml , 680
TBShopCar/TBShopCar/res/layout/include_topbar.xml , 1339
TBShopCar/TBShopCar/res/layout/item_elv_child_test.xml , 6150
TBShopCar/TBShopCar/res/layout/item_elv_group_test.xml , 2423
TBShopCar/TBShopCar/res/layout/item_rv_test.xml , 408
TBShopCar/TBShopCar/res/layout/ui_alert_view.xml , 1909
TBShopCar/TBShopCar/res/values/colors.xml , 337
TBShopCar/TBShopCar/res/values/dimens.xml , 220
TBShopCar/TBShopCar/res/values/strings.xml , 357
TBShopCar/TBShopCar/res/values/styles.xml , 1227
TBShopCar/TBShopCar/res/values-v11/styles.xml , 334
TBShopCar/TBShopCar/res/values-v14/styles.xml , 391
TBShopCar/TBShopCar/res/values-w820dp/dimens.xml , 381
TBShopCar/TBShopCar/src/com/jock/tbshopcar/adapter/MyExpandableListAdapter.java , 13763
TBShopCar/TBShopCar/src/com/jock/tbshopcar/dao/DBHelper.java , 1569
TBShopCar/TBShopCar/src/com/jock/tbshopcar/dao/ShoppingCartDao.java , 5345
TBShopCar/TBShopCar/src/com/jock/tbshopcar/entity/ShoppingCartBean.java , 6369
TBShopCar/TBShopCar/src/com/jock/tbshopcar/listener/OnShoppingCartChangeListener.java , 198
TBShopCar/TBShopCar/src/com/jock/tbshopcar/listener/ResponseCallBack.java , 126
TBShopCar/TBShopCar/src/com/jock/tbshopcar/listener/ShoppingCartBiz.java , 8510
TBShopCar/TBShopCar/src/com/jock/tbshopcar/listener/ShoppingCartHttpBiz.java , 3404
TBShopCar/TBShopCar/src/com/jock/tbshopcar/ShoppingCartActivity.java , 6805
TBShopCar/TBShopCar/src/com/jock/tbshopcar/utils/DecimalUtil.java , 3014
TBShopCar/TBShopCar/src/com/jock/tbshopcar/utils/JsonReponseHandler.java , 1711
TBShopCar/TBShopCar/src/com/jock/tbshopcar/utils/ToastHelper.java , 2655
TBShopCar/TBShopCar/src/com/jock/tbshopcar/view/UIAlertView.java , 3028
没有账号? 忘记密码?

社交账号快速登录