当前位置:   article > 正文

Android -- 购物车_android 购物车

android 购物车

简单购物车案例

购物车功能描述

        第一次进入购物车页面,购物车里面是空的,同时提示去逛手机商场, 如 首次进入的页面 图所示。接着去商场页面选购手机,随便挑了几部手机加入购物车,再返回购物车页面,即可看 到购物车的商品列表,如 购物车已选列表图 所示,有商品图片、名称、数量、单价、总价等等信息。当然购物车并 不仅仅只是展示待购买的商品,还要支持最终购买的结算操作、支持清空购物车,长按删除单项订单等功能。

        购物车的存在感很强,不仅仅在购物车页面才能看到购物车。往往在商场页面,甚至商品详情页面,都 会看到某个角落冒出购物车图标。一旦有新商品加入购物车,购物车图标上的商品数量立马加一。当然,用户也能点击购物车图标直接跳到购物车页面。商场页面除了商品列表之外,页面右上角还有一个购物车图标,用户把商品加购物车,那么图标上的数字也会加一。

功能需求

  • 购物车存放着用户准备购买的商品,一开始是空的,随着商品被加入购物车,购物车中就会显示已添加的商品列表。

  • 除了购物车页面,其它页面(如商场频道页面、商品详情页面),都可能在右上角或者右 下角找到购物车图标。购物车图标上会显示已添加的商品数量,且商品数量是实时更新的。

  • 购物车页面、商场频道页面、商品详情页面多处都会显示商品的小图或者大图

界面设计

  • 线性布局LinearLayout:购物车界面从上往下排列,垂直方向的线性布局

  • 网格布局GridLayout:商场页面的陈列橱柜,允许分行分列展示商品

  • 相对布局RelativeLayout:页面右上角的购物车图标,图标右上角又有数字标记,按照指定方位排列控件

  • 其他常见控件尚有文本视图TextView、图像视图ImageView,按钮控件Button等

存储技术

  • 数据库SQLite:最直观的是数据库,购物车里的商品列表一定放在SQLite中,增删改查

  • 全局内存:购物车图标右上角的数字表示购物车中的商品数量,该数值建议保存在全局内存中,这样不必每次都到数据库中执行count操作。

  • 存储卡文件:App把下载的商品图片保存在存储卡中,这样下次就能直接从存储卡获取商品图片,加快浏览速度。

  • 共享参数SharedPreferences:是否首次访问网络图片,这个标志位推荐放在共享参数中, 需要持久化存储,并且只有一个参数信息

效果图

首次进入的页面图

 

商品展示列表图

购物车已选列表图

 手机详情页面图

长按删除订单图

图片

主程序

ShoppingChannelActivity.java
  1. package com.kcs.shoppingcart;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.GridView;
  6. import android.widget.TextView;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import com.kcs.shoppingcart.adapter.GoodsAdapter;
  9. import com.kcs.shoppingcart.datebase.ShoppingDBHelper;
  10. import com.kcs.shoppingcart.entity.GoodsInfo;
  11. import com.kcs.shoppingcart.utils.ToastUtil;
  12. import java.util.List;
  13. public class ShoppingChannelActivity extends AppCompatActivity implements View.OnClickListener, GoodsAdapter.AddCartListener {
  14. /**
  15. * 声明一个商品数据库的帮助器对象
  16. */
  17. private ShoppingDBHelper mDBHelper;
  18. private TextView tv_count;
  19. private GridView gv_channel;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_shopping_channel);
  24. TextView tv_title = findViewById(R.id.tv_title);
  25. tv_title.setText("手机商场");
  26. tv_count = findViewById(R.id.tv_count);
  27. gv_channel = findViewById(R.id.gv_channel);
  28. findViewById(R.id.iv_back).setOnClickListener(this);
  29. findViewById(R.id.iv_cart).setOnClickListener(this);
  30. mDBHelper = ShoppingDBHelper.getInstance(this);
  31. mDBHelper.openReadLink();
  32. mDBHelper.openWriteLink();
  33. // 从数据库查询出商品信息,并展示
  34. showGoods();
  35. }
  36. @Override
  37. protected void onResume() {
  38. super.onResume();
  39. // 查询购物车商品总数,并展示
  40. showCartInfoTotal();
  41. }
  42. /**
  43. * 查询购物车商品总数,并展示
  44. */
  45. private void showCartInfoTotal() {
  46. int count = mDBHelper.countCartInfo();
  47. MyApplication.getInstance().goodsCount = count;
  48. tv_count.setText(String.valueOf(count));
  49. }
  50. private void showGoods() {
  51. // 查询商品数据库中的所有商品记录
  52. List<GoodsInfo> list = mDBHelper.queryAllGoodsInfo();
  53. GoodsAdapter adapter = new GoodsAdapter(this, list,this);
  54. gv_channel.setAdapter(adapter);
  55. }
  56. /**
  57. * 把指定编号的商品添加到购物车
  58. * @param goodsId
  59. * @param goodsName
  60. */
  61. @Override
  62. public void addToCart(int goodsId, String goodsName) {
  63. // 购物车商品数量+1
  64. int count = ++MyApplication.getInstance().goodsCount;
  65. tv_count.setText(String.valueOf(count));
  66. mDBHelper.insertCartInfo(goodsId);
  67. ToastUtil.show(this, "已添加一部" + goodsName + "到购物车");
  68. }
  69. @Override
  70. protected void onDestroy() {
  71. super.onDestroy();
  72. mDBHelper.closeLink();
  73. }
  74. @Override
  75. public void onClick(View v) {
  76. switch (v.getId()) {
  77. case R.id.iv_back:
  78. // 点击了返回图标,关闭当前页面
  79. finish();
  80. break;
  81. case R.id.iv_cart:
  82. // 点击了购物车图标
  83. // 从商场页面跳到购物车页面
  84. Intent intent = new Intent(this, ShoppingCartActivity.class);
  85. // 设置启动标志,避免多次返回同一页面的
  86. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  87. startActivity(intent);
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. }
ShoppingDetailActivity.java
  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.ImageView;
  7. import android.widget.TextView;
  8. import com.dongnaoedu.chapter06.database.ShoppingDBHelper;
  9. import com.dongnaoedu.chapter06.enity.GoodsInfo;
  10. import com.dongnaoedu.chapter06.util.ToastUtil;
  11. public class ShoppingDetailActivity extends AppCompatActivity implements View.OnClickListener {
  12. private TextView tv_title;
  13. private TextView tv_count;
  14. private TextView tv_goods_price;
  15. private TextView tv_goods_desc;
  16. private ImageView iv_goods_pic;
  17. private ShoppingDBHelper mDBHelper;
  18. private int mGoodsId;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_shopping_detail);
  23. tv_title = findViewById(R.id.tv_title);
  24. tv_count = findViewById(R.id.tv_count);
  25. tv_goods_price = findViewById(R.id.tv_goods_price);
  26. tv_goods_desc = findViewById(R.id.tv_goods_desc);
  27. iv_goods_pic = findViewById(R.id.iv_goods_pic);
  28. findViewById(R.id.iv_back).setOnClickListener(this);
  29. findViewById(R.id.iv_cart).setOnClickListener(this);
  30. findViewById(R.id.btn_add_cart).setOnClickListener(this);
  31. tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
  32. mDBHelper = ShoppingDBHelper.getInstance(this);
  33. }
  34. @Override
  35. protected void onResume() {
  36. super.onResume();
  37. showDetail();
  38. }
  39. private void showDetail() {
  40. // 获取上一个页面传来的商品编号
  41. mGoodsId = getIntent().getIntExtra("goods_id", 0);
  42. if (mGoodsId > 0) {
  43. // 根据商品编号查询商品数据库中的商品记录
  44. GoodsInfo info = mDBHelper.queryGoodsInfoById(mGoodsId);
  45. tv_title.setText(info.name);
  46. tv_goods_desc.setText(info.description);
  47. tv_goods_price.setText(String.valueOf((int) info.price));
  48. iv_goods_pic.setImageURI(Uri.parse(info.picPath));
  49. }
  50. }
  51. @Override
  52. public void onClick(View v) {
  53. switch (v.getId()) {
  54. case R.id.iv_back:
  55. finish();
  56. break;
  57. case R.id.iv_cart:
  58. Intent intent = new Intent(this, ShoppingCartActivity.class);
  59. startActivity(intent);
  60. break;
  61. case R.id.btn_add_cart:
  62. addToCart(mGoodsId);
  63. break;
  64. }
  65. }
  66. private void addToCart(int goodsId) {
  67. // 购物车商品数量+1
  68. int count = ++MyApplication.getInstance().goodsCount;
  69. tv_count.setText(String.valueOf(count));
  70. mDBHelper.insertCartInfo(goodsId);
  71. ToastUtil.show(this, "成功添加至购物车");
  72. }
  73. }
ShoppingCartActivity.java
  1. package com.kcs.shoppingcart;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.LinearLayout;
  7. import android.widget.ListView;
  8. import android.widget.TextView;
  9. import androidx.appcompat.app.AlertDialog;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import com.kcs.shoppingcart.adapter.CartAdapter;
  12. import com.kcs.shoppingcart.datebase.ShoppingDBHelper;
  13. import com.kcs.shoppingcart.entity.CartInfo;
  14. import com.kcs.shoppingcart.entity.GoodsInfo;
  15. import com.kcs.shoppingcart.utils.ToastUtil;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. public class ShoppingCartActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
  20. private TextView tv_count;
  21. private ListView lv_cart;
  22. private ShoppingDBHelper mDBHelper;
  23. /**
  24. * 声明一个购物车中的商品信息列表
  25. */
  26. private List<CartInfo> mCartList;
  27. /**
  28. * 声明一个根据商品编号查找商品信息的映射,把商品信息缓存起来,这样不用每一次都去查询数据库
  29. */
  30. private Map<Integer, GoodsInfo> mGoodsMap = new HashMap<>();
  31. private TextView tv_total_price;
  32. private LinearLayout ll_empty;
  33. private LinearLayout ll_content;
  34. private CartAdapter mCartAdapter;
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_shopping_cart);
  39. TextView tv_title = findViewById(R.id.tv_title);
  40. tv_title.setText("购物车");
  41. lv_cart = findViewById(R.id.lv_cart);
  42. tv_total_price = findViewById(R.id.tv_total_price);
  43. tv_count = findViewById(R.id.tv_count);
  44. tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
  45. mDBHelper = ShoppingDBHelper.getInstance(this);
  46. findViewById(R.id.iv_back).setOnClickListener(this);
  47. findViewById(R.id.btn_shopping_channel).setOnClickListener(this);
  48. findViewById(R.id.btn_clear).setOnClickListener(this);
  49. findViewById(R.id.btn_settle).setOnClickListener(this);
  50. ll_empty = findViewById(R.id.ll_empty);
  51. ll_content = findViewById(R.id.ll_content);
  52. }
  53. @Override
  54. protected void onResume() {
  55. super.onResume();
  56. showCart();
  57. }
  58. /**
  59. * 展示购物车中的商品列表
  60. */
  61. private void showCart() {
  62. // 查询购物车数据库中所有的商品记录
  63. mCartList = mDBHelper.queryAllCartInfo();
  64. if (mCartList.size() == 0) {
  65. return;
  66. }
  67. for (CartInfo info : mCartList) {
  68. // 根据商品编号查询商品数据库中的商品记录
  69. GoodsInfo goods = mDBHelper.queryGoodsInfoById(info.goodsId);
  70. mGoodsMap.put(info.goodsId, goods);
  71. info.goods = goods;
  72. }
  73. mCartAdapter = new CartAdapter(this, mCartList);
  74. lv_cart.setAdapter(mCartAdapter);
  75. // 给商品行添加点击事件。点击商品行跳到商品的详情页
  76. lv_cart.setOnItemClickListener(this);
  77. // 给商品行添加长按事件。长按商品行就删除该商品
  78. lv_cart.setOnItemLongClickListener(this);
  79. // 重新计算购物车中的商品总金额
  80. refreshTotalPrice();
  81. }
  82. private void deleteGoods(CartInfo info) {
  83. MyApplication.getInstance().goodsCount -= info.count;
  84. // 从购物车的数据库中删除商品
  85. mDBHelper.deleteCartInfoByGoodsId(info.goodsId);
  86. // 从购物车的列表中删除商品
  87. CartInfo removed = null;
  88. for (CartInfo cartInfo : mCartList) {
  89. if (cartInfo.goodsId == info.goodsId) {
  90. removed = cartInfo;
  91. break;
  92. }
  93. }
  94. mCartList.remove(removed);
  95. // 显示最新的商品数量
  96. showCount();
  97. ToastUtil.show(this, "已从购物车删除" + mGoodsMap.get(info.goodsId).name);
  98. mGoodsMap.remove(info.goodsId);
  99. // 刷新购物车中所有商品的总金额
  100. refreshTotalPrice();
  101. }
  102. /**
  103. * 显示购物车图标中的商品数量
  104. */
  105. private void showCount() {
  106. tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
  107. // 购物车中没有商品,显示“空空如也”
  108. if (MyApplication.getInstance().goodsCount == 0) {
  109. ll_empty.setVisibility(View.VISIBLE);
  110. ll_content.setVisibility(View.GONE);
  111. // 通知适配器发生了数据变化
  112. mCartAdapter.notifyDataSetChanged();
  113. } else {
  114. ll_content.setVisibility(View.VISIBLE);
  115. ll_empty.setVisibility(View.GONE);
  116. }
  117. }
  118. /**
  119. * 重新计算购物车中的商品总金额
  120. */
  121. private void refreshTotalPrice() {
  122. int totalPrice = 0;
  123. for (CartInfo info : mCartList) {
  124. GoodsInfo goods = mGoodsMap.get(info.goodsId);
  125. totalPrice += goods.price * info.count;
  126. }
  127. tv_total_price.setText(String.valueOf(totalPrice));
  128. }
  129. @Override
  130. public void onClick(View v) {
  131. switch (v.getId()) {
  132. case R.id.iv_back:
  133. // 点击了返回图标
  134. // 关闭当前页面
  135. finish();
  136. break;
  137. case R.id.btn_shopping_channel:
  138. // 从购物车页面跳到商场页面
  139. Intent intent = new Intent(this, com.kcs.shoppingcart.ShoppingChannelActivity.class);
  140. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  141. startActivity(intent);
  142. break;
  143. case R.id.btn_clear:
  144. // 清空购物车数据库
  145. mDBHelper.deleteAllCartInfo();
  146. MyApplication.getInstance().goodsCount = 0;
  147. // 显示最新的商品数量
  148. showCount();
  149. ToastUtil.show(this, "购物车已清空");
  150. break;
  151. case R.id.btn_settle:
  152. // 点击了“结算”按钮
  153. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  154. builder.setTitle("结算商品");
  155. builder.setMessage("客官抱歉,支付功能尚未开通");
  156. builder.setPositiveButton("我知道了", null);
  157. builder.create().show();
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. /**
  164. * 给商品行添加点击事件。点击商品行跳到商品的详情页
  165. */
  166. @Override
  167. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  168. Intent intent = new Intent(ShoppingCartActivity.this, com.kcs.shoppingcart.ShoppingDetailActivity.class);
  169. intent.putExtra("goods_id", mCartList.get(position).goodsId);
  170. startActivity(intent);
  171. }
  172. /**
  173. *给商品行添加长按事件。长按商品行就删除该商品
  174. */
  175. @Override
  176. public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  177. CartInfo info = mCartList.get(position);
  178. AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingCartActivity.this);
  179. builder.setMessage("是否从购物车删除" + info.goods.name + "?");
  180. builder.setPositiveButton("是", (dialog, which) -> {
  181. // 从集合中移除数据
  182. mCartList.remove(position);
  183. // 通知适配器发生了数据变化
  184. mCartAdapter.notifyDataSetChanged();
  185. // 删除该商品
  186. deleteGoods(info);
  187. });
  188. builder.setNegativeButton("否", null);
  189. builder.create().show();
  190. return true;
  191. }
  192. }

MyApplication.java

  1. package com.dongnaoedu.chapter06;
  2. import android.app.Application;
  3. import android.content.res.Configuration;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.os.Environment;
  7. import android.util.Log;
  8. import androidx.annotation.NonNull;
  9. import androidx.room.Room;
  10. import com.dongnaoedu.chapter06.database.ShoppingDBHelper;
  11. import com.dongnaoedu.chapter06.enity.GoodsInfo;
  12. import com.dongnaoedu.chapter06.util.FileUtil;
  13. import com.dongnaoedu.chapter06.util.SharedUtil;
  14. import java.io.File;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. public class MyApplication extends Application {
  18. // 购物车中的商品总数量
  19. public int goodsCount;
  20. public static MyApplication getInstance() {
  21. return mApp;
  22. }
  23. //在App启动时调用
  24. @Override
  25. public void onCreate() {
  26. super.onCreate();
  27. mApp = this;
  28. Log.d("ning", "MyApplication onCreate");
  29. // 初始化商品信息
  30. initGoodsInfo();
  31. }
  32. private void initGoodsInfo() {
  33. // 获取共享参数保存的是否首次打开参数
  34. boolean isFirst = SharedUtil.getInstance(this).readBoolean("first", true);
  35. // 获取当前App的私有下载路径
  36. String directory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + File.separatorChar;
  37. if (isFirst) {
  38. // 模拟网络图片下载
  39. List<GoodsInfo> list = GoodsInfo.getDefaultList();
  40. for (GoodsInfo info : list) {
  41. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), info.pic);
  42. String path = directory + info.id + ".jpg";
  43. // 往存储卡保存商品图片
  44. FileUtil.saveImage(path, bitmap);
  45. // 回收位图对象
  46. bitmap.recycle();
  47. info.picPath = path;
  48. }
  49. // 打开数据库,把商品信息插入到表中
  50. ShoppingDBHelper dbHelper = ShoppingDBHelper.getInstance(this);
  51. dbHelper.openWriteLink();
  52. dbHelper.insertGoodsInfos(list);
  53. dbHelper.closeLink();
  54. // 把是否首次打开写入共享参数
  55. SharedUtil.getInstance(this).writeBoolean("first", false);
  56. }
  57. }
  58. }

适配器

 package:adapter

CartAdapter.java
  1. package com.kcs.shoppingcart.adapter;
  2. import android.content.Context;
  3. import android.net.Uri;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10. import com.kcs.shoppingcart.R;
  11. import com.kcs.shoppingcart.entity.CartInfo;
  12. import java.util.List;
  13. public class CartAdapter extends BaseAdapter {
  14. private Context mContext;
  15. private List<CartInfo> mCartList;
  16. public CartAdapter(Context mContext, List<CartInfo> mCartList) {
  17. this.mContext = mContext;
  18. this.mCartList = mCartList;
  19. }
  20. @Override
  21. public int getCount() {
  22. return mCartList.size();
  23. }
  24. @Override
  25. public Object getItem(int position) {
  26. return mCartList.get(position);
  27. }
  28. @Override
  29. public long getItemId(int position) {
  30. return position;
  31. }
  32. @Override
  33. public View getView(int position, View convertView, ViewGroup parent) {
  34. ViewHolder holder;
  35. if (convertView == null) {
  36. holder = new ViewHolder();
  37. // 获取布局文件item_cart.xml的根视图
  38. convertView = LayoutInflater.from(mContext).inflate(R.layout.item_cart, null);
  39. holder.iv_thumb = convertView.findViewById(R.id.iv_thumb);
  40. holder.tv_name = convertView.findViewById(R.id.tv_name);
  41. holder.tv_desc = convertView.findViewById(R.id.tv_desc);
  42. holder.tv_count = convertView.findViewById(R.id.tv_count);
  43. holder.tv_price = convertView.findViewById(R.id.tv_price);
  44. holder.tv_sum = convertView.findViewById(R.id.tv_sum);
  45. convertView.setTag(holder);
  46. } else {
  47. holder = (ViewHolder) convertView.getTag();
  48. }
  49. CartInfo info = mCartList.get(position);
  50. holder.iv_thumb.setImageURI(Uri.parse(info.goods.picPath));
  51. holder.tv_name.setText(info.goods.name);
  52. holder.tv_desc.setText(info.goods.description);
  53. holder.tv_count.setText(String.valueOf(info.count));
  54. holder.tv_price.setText(String.valueOf((int) info.goods.price));
  55. // 设置商品总价
  56. holder.tv_sum.setText(String.valueOf((int) (info.count * info.goods.price)));
  57. return convertView;
  58. }
  59. public final class ViewHolder {
  60. public ImageView iv_thumb;
  61. public TextView tv_name;
  62. public TextView tv_desc;
  63. public TextView tv_count;
  64. public TextView tv_price;
  65. public TextView tv_sum;
  66. }
  67. }

GoodsAdapter.java

  1. package com.kcs.shoppingcart.adapter;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.Button;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12. import com.kcs.shoppingcart.R;
  13. import com.kcs.shoppingcart.ShoppingDetailActivity;
  14. import com.kcs.shoppingcart.entity.GoodsInfo;
  15. import java.util.List;
  16. public class GoodsAdapter extends BaseAdapter {
  17. private Context mContext;
  18. private List<GoodsInfo> mGoodsInfo;
  19. public GoodsAdapter(Context mContext, List<GoodsInfo> mGoodsInfo, AddCartListener mAddCartListener) {
  20. this.mContext = mContext;
  21. this.mGoodsInfo = mGoodsInfo;
  22. this.mAddCartListener = mAddCartListener;
  23. }
  24. @Override
  25. public int getCount() {
  26. return mGoodsInfo.size();
  27. }
  28. @Override
  29. public Object getItem(int position) {
  30. return position;
  31. }
  32. @Override
  33. public long getItemId(int position) {
  34. return position;
  35. }
  36. @Override
  37. public View getView(int position, View convertView, ViewGroup parent) {
  38. GoodsInfo info = mGoodsInfo.get(position);
  39. ViewHolder holder;
  40. if (convertView == null) {
  41. convertView = LayoutInflater.from(mContext).inflate(R.layout.item_goods, null);
  42. holder = new ViewHolder();
  43. holder.iv_thumb = convertView.findViewById(R.id.iv_thumb);
  44. holder.tv_name = convertView.findViewById(R.id.tv_name);
  45. holder.tv_price = convertView.findViewById(R.id.tv_price);
  46. holder.btn_add = convertView.findViewById(R.id.btn_add);
  47. convertView.setTag(holder);
  48. } else {
  49. holder = (ViewHolder) convertView.getTag();
  50. }
  51. // 给控件设置值
  52. holder.iv_thumb.setImageURI(Uri.parse(info.picPath));
  53. holder.tv_name.setText(info.name);
  54. holder.tv_price.setText(String.valueOf((int) info.price));
  55. // 添加到购物车
  56. holder.btn_add.setOnClickListener(v -> {
  57. mAddCartListener.addToCart(info.id, info.name);
  58. });
  59. //点击商品图片,跳转到商品详情页面
  60. holder.iv_thumb.setOnClickListener(v -> {
  61. Intent intent = new Intent(mContext, ShoppingDetailActivity.class);
  62. intent.putExtra("goods_id", info.id);
  63. mContext.startActivity(intent);
  64. });
  65. return convertView;
  66. }
  67. public final class ViewHolder {
  68. public ImageView iv_thumb;
  69. public TextView tv_name;
  70. public TextView tv_price;
  71. public Button btn_add;
  72. }
  73. /**
  74. * 声明一个加入购物车的监听器对象
  75. */
  76. private AddCartListener mAddCartListener;
  77. /**
  78. * 定义一个加入购物车的监听器接口
  79. */
  80. public interface AddCartListener {
  81. void addToCart(int goodsId, String goodsName);
  82. }
  83. }

工具类

package util:

ToastUitl.java

  1. package com.dongnaoedu.chapter06.util;
  2. import android.content.Context;
  3. import android.widget.Toast;
  4. public class ToastUtil {
  5. public static void show(Context ctx, String desc) {
  6. Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
  7. }
  8. }

SharedUtil.java

  1. import android.content.Context;
  2. import android.content.SharedPreferences;
  3. public class SharedUtil {
  4. private static SharedUtil mUtil;
  5. private SharedPreferences preferences;
  6. public static SharedUtil getInstance(Context ctx) {
  7. if (mUtil == null) {
  8. mUtil = new SharedUtil();
  9. mUtil.preferences = ctx.getSharedPreferences("shopping", Context.MODE_PRIVATE);
  10. }
  11. return mUtil;
  12. }
  13. public void writeBoolean(String key, boolean value) {
  14. SharedPreferences.Editor editor = preferences.edit();
  15. editor.putBoolean(key, value);
  16. editor.commit();
  17. }
  18. public boolean readBoolean(String key, boolean defaultValue) {
  19. return preferences.getBoolean(key, defaultValue);
  20. }
  21. }

FileUtil.java

  1. import android.graphics.Bitmap;
  2. import android.graphics.BitmapFactory;
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.FileReader;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. public class FileUtil {
  12. // 把位图数据保存到指定路径的图片文件
  13. public static void saveImage(String path, Bitmap bitmap) {
  14. FileOutputStream fos = null;
  15. try {
  16. fos = new FileOutputStream(path);
  17. // 把位图数据压缩到文件输出流中
  18. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. } finally {
  22. if (fos != null) {
  23. try {
  24. fos.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. }
  31. // 从指定路径的图片文件中读取位图数据
  32. public static Bitmap openImage(String path) {
  33. Bitmap bitmap = null;
  34. FileInputStream fis = null;
  35. try {
  36. fis = new FileInputStream(path);
  37. bitmap = BitmapFactory.decodeStream(fis);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. } finally {
  41. if (fis != null) {
  42. try {
  43. fis.close();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49. return bitmap;
  50. }
  51. }

实体类 

package pojo:

CarInfo.java

  1. package com.kcs.shoppingcart.entity;
  2. //购物车信息
  3. public class CartInfo {
  4. public int id;
  5. /**
  6. * 商品编号
  7. */
  8. public int goodsId;
  9. /**
  10. * 商品数量
  11. */
  12. public int count;
  13. /**
  14. * 商品信息
  15. */
  16. public GoodsInfo goods;
  17. public CartInfo() {
  18. }
  19. public CartInfo(int id, int goodsId, int count) {
  20. this.id = id;
  21. this.goodsId = goodsId;
  22. this.count = count;
  23. this.goods = new GoodsInfo();
  24. }
  25. }
GoodsInfo.java
  1. import com.dongnaoedu.chapter06.R;
  2. import java.util.ArrayList;
  3. public class GoodsInfo {
  4. public int id;
  5. // 名称
  6. public String name;
  7. // 描述
  8. public String description;
  9. // 价格
  10. public float price;
  11. // 大图的保存路径
  12. public String picPath;
  13. // 大图的资源编号
  14. public int pic;
  15. // 声明一个手机商品的名称数组
  16. private static String[] mNameArray = {
  17. "iPhone11", "Mate30", "小米10", "OPPO Reno3", "vivo X30", "荣耀30S"
  18. };
  19. // 声明一个手机商品的描述数组
  20. private static String[] mDescArray = {
  21. "Apple iPhone11 256GB 绿色 4G全网通手机",
  22. "华为 HUAWEI Mate30 8GB+256GB 丹霞橙 5G全网通 全面屏手机",
  23. "小米 MI10 8GB+128GB 钛银黑 5G手机 游戏拍照手机",
  24. "OPPO Reno3 8GB+128GB 蓝色星夜 双模5G 拍照游戏智能手机",
  25. "vivo X30 8GB+128GB 绯云 5G全网通 美颜拍照手机",
  26. "荣耀30S 8GB+128GB 蝶羽红 5G芯片 自拍全面屏手机"
  27. };
  28. // 声明一个手机商品的价格数组
  29. private static float[] mPriceArray = {6299, 4999, 3999, 2999, 2998, 2399};
  30. // 声明一个手机商品的大图数组
  31. private static int[] mPicArray = {
  32. R.drawable.iphone, R.drawable.huawei, R.drawable.xiaomi,
  33. R.drawable.oppo, R.drawable.vivo, R.drawable.rongyao
  34. };
  35. // 获取默认的手机信息列表
  36. public static ArrayList<GoodsInfo> getDefaultList() {
  37. ArrayList<GoodsInfo> goodsList = new ArrayList<GoodsInfo>();
  38. for (int i = 0; i < mNameArray.length; i++) {
  39. GoodsInfo info = new GoodsInfo();
  40. info.id = i;
  41. info.name = mNameArray[i];
  42. info.description = mDescArray[i];
  43. info.price = mPriceArray[i];
  44. info.pic = mPicArray[i];
  45. goodsList.add(info);
  46. }
  47. return goodsList;
  48. }
  49. }

购物车数据库类

package database:

  1. import android.content.ContentValues;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import com.dongnaoedu.chapter06.enity.CartInfo;
  7. import com.dongnaoedu.chapter06.enity.GoodsInfo;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class ShoppingDBHelper extends SQLiteOpenHelper {
  11. private static final String DB_NAME = "shopping.db";
  12. // 商品信息表
  13. private static final String TABLE_GOODS_INFO = "goods_info";
  14. // 购物车信息表
  15. private static final String TABLE_CART_INFO = "cart_info";
  16. private static final int DB_VERSION = 1;
  17. private static ShoppingDBHelper mHelper = null;
  18. private SQLiteDatabase mRDB = null;
  19. private SQLiteDatabase mWDB = null;
  20. private ShoppingDBHelper(Context context) {
  21. super(context, DB_NAME, null, DB_VERSION);
  22. }
  23. // 利用单例模式获取数据库帮助器的唯一实例
  24. public static ShoppingDBHelper getInstance(Context context) {
  25. if (mHelper == null) {
  26. mHelper = new ShoppingDBHelper(context);
  27. }
  28. return mHelper;
  29. }
  30. // 打开数据库的读连接
  31. public SQLiteDatabase openReadLink() {
  32. if (mRDB == null || !mRDB.isOpen()) {
  33. mRDB = mHelper.getReadableDatabase();
  34. }
  35. return mRDB;
  36. }
  37. // 打开数据库的写连接
  38. public SQLiteDatabase openWriteLink() {
  39. if (mWDB == null || !mWDB.isOpen()) {
  40. mWDB = mHelper.getWritableDatabase();
  41. }
  42. return mWDB;
  43. }
  44. // 关闭数据库连接
  45. public void closeLink() {
  46. if (mRDB != null && mRDB.isOpen()) {
  47. mRDB.close();
  48. mRDB = null;
  49. }
  50. if (mWDB != null && mWDB.isOpen()) {
  51. mWDB.close();
  52. mWDB = null;
  53. }
  54. }
  55. // 创建数据库,执行建表语句
  56. @Override
  57. public void onCreate(SQLiteDatabase db) {
  58. // 创建商品信息表
  59. String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_GOODS_INFO +
  60. "(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
  61. " name VARCHAR NOT NULL," +
  62. " description VARCHAR NOT NULL," +
  63. " price FLOAT NOT NULL," +
  64. " pic_path VARCHAR NOT NULL);";
  65. db.execSQL(sql);
  66. // 创建购物车信息表
  67. sql = "CREATE TABLE IF NOT EXISTS " + TABLE_CART_INFO +
  68. "(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
  69. " goods_id INTEGER NOT NULL," +
  70. " count INTEGER NOT NULL);";
  71. db.execSQL(sql);
  72. }
  73. @Override
  74. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  75. }
  76. // 添加多条商品信息
  77. public void insertGoodsInfos(List<GoodsInfo> list) {
  78. // 插入多条记录,要么全部成功,要么全部失败
  79. try {
  80. mWDB.beginTransaction();
  81. for (GoodsInfo info : list) {
  82. ContentValues values = new ContentValues();
  83. values.put("name", info.name);
  84. values.put("description", info.description);
  85. values.put("price", info.price);
  86. values.put("pic_path", info.picPath);
  87. mWDB.insert(TABLE_GOODS_INFO, null, values);
  88. }
  89. mWDB.setTransactionSuccessful();
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. } finally {
  93. mWDB.endTransaction();
  94. }
  95. }
  96. // 查询所有的商品信息
  97. public List<GoodsInfo> queryAllGoodsInfo() {
  98. String sql = "select * from " + TABLE_GOODS_INFO;
  99. List<GoodsInfo> list = new ArrayList<>();
  100. Cursor cursor = mRDB.rawQuery(sql, null);
  101. while (cursor.moveToNext()) {
  102. GoodsInfo info = new GoodsInfo();
  103. info.id = cursor.getInt(0);
  104. info.name = cursor.getString(1);
  105. info.description = cursor.getString(2);
  106. info.price = cursor.getFloat(3);
  107. info.picPath = cursor.getString(4);
  108. list.add(info);
  109. }
  110. cursor.close();
  111. return list;
  112. }
  113. // 添加商品到购物车
  114. public void insertCartInfo(int goodsId) {
  115. // 如果购物车中不存在该商品,添加一条信息
  116. CartInfo cartInfo = queryCartInfoByGoodsId(goodsId);
  117. ContentValues values = new ContentValues();
  118. values.put("goods_id", goodsId);
  119. if (cartInfo == null) {
  120. values.put("count", 1);
  121. mWDB.insert(TABLE_CART_INFO, null, values);
  122. } else {
  123. // 如果购物车中已经存在该商品,更新商品数量
  124. values.put("_id", cartInfo.id);
  125. values.put("count", ++cartInfo.count);
  126. mWDB.update(TABLE_CART_INFO, values, "_id=?", new String[]{String.valueOf(cartInfo.id)});
  127. }
  128. }
  129. // 根据商品信息ID查询购物车信息
  130. private CartInfo queryCartInfoByGoodsId(int goodsId) {
  131. Cursor cursor = mRDB.query(TABLE_CART_INFO, null, "goods_id=?", new String[]{String.valueOf(goodsId)}, null, null, null);
  132. CartInfo info = null;
  133. if (cursor.moveToNext()) {
  134. info = new CartInfo();
  135. info.id = cursor.getInt(0);
  136. info.goodsId = cursor.getInt(1);
  137. info.count = cursor.getInt(2);
  138. }
  139. return info;
  140. }
  141. // 统计购物车中商品的总数量
  142. public int countCartInfo() {
  143. int count = 0;
  144. String sql = "select sum(count) from " + TABLE_CART_INFO;
  145. Cursor cursor = mRDB.rawQuery(sql, null);
  146. if (cursor.moveToNext()) {
  147. count = cursor.getInt(0);
  148. }
  149. return count;
  150. }
  151. // 查询购物车中所有的信息列表
  152. public List<CartInfo> queryAllCartInfo() {
  153. List<CartInfo> list = new ArrayList<>();
  154. Cursor cursor = mRDB.query(TABLE_CART_INFO, null, null, null, null, null, null);
  155. while (cursor.moveToNext()) {
  156. CartInfo info = new CartInfo();
  157. info.id = cursor.getInt(0);
  158. info.goodsId = cursor.getInt(1);
  159. info.count = cursor.getInt(2);
  160. list.add(info);
  161. }
  162. return list;
  163. }
  164. // 根据商品ID查询商品信息
  165. public GoodsInfo queryGoodsInfoById(int goodsId) {
  166. GoodsInfo info = null;
  167. Cursor cursor = mRDB.query(TABLE_GOODS_INFO, null, "_id=?", new String[]{String.valueOf(goodsId)}, null, null, null);
  168. if (cursor.moveToNext()) {
  169. info = new GoodsInfo();
  170. info.id = cursor.getInt(0);
  171. info.name = cursor.getString(1);
  172. info.description = cursor.getString(2);
  173. info.price = cursor.getFloat(3);
  174. info.picPath = cursor.getString(4);
  175. }
  176. return info;
  177. }
  178. // 根据商品ID删除购物车信息
  179. public void deleteCartInfoByGoodsId(int goodsId) {
  180. mWDB.delete(TABLE_CART_INFO, "goods_id=?", new String[]{String.valueOf(goodsId)});
  181. }
  182. // 删除所有购物车信息
  183. public void deleteAllCartInfo() {
  184. mWDB.delete(TABLE_CART_INFO, "1=1", null);
  185. }
  186. }

布局文件

activity_shopping_channel.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:background="@color/orange"
  5. android:orientation="vertical" >
  6. <include layout="@layout/title_shopping" />
  7. <GridView
  8. android:id="@+id/gv_channel"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:numColumns="2" />
  12. </LinearLayout>

activity_shopping_detail.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/orange"
  6. android:orientation="vertical">
  7. <include layout="@layout/title_shopping" />
  8. <ScrollView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content">
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="vertical">
  15. <ImageView
  16. android:id="@+id/iv_goods_pic"
  17. android:layout_width="match_parent"
  18. android:layout_height="350dp"
  19. android:scaleType="fitCenter"
  20. tools:src="@drawable/xiaomi" />
  21. <TextView
  22. android:id="@+id/tv_goods_price"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:paddingLeft="5dp"
  26. android:textColor="@color/red"
  27. android:textSize="22sp"
  28. tools:text="1990" />
  29. <TextView
  30. android:id="@+id/tv_goods_desc"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:paddingLeft="5dp"
  34. android:textColor="@color/black"
  35. android:textSize="15sp"
  36. tools:text="小米 MI10 8GB+128GB 钛银黑 5G手机 游戏拍照手机" />
  37. <Button
  38. android:id="@+id/btn_add_cart"
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:text="加入购物车"
  42. android:textColor="@color/black"
  43. android:textSize="17sp" />
  44. </LinearLayout>
  45. </ScrollView>
  46. </LinearLayout>

activity_shopping_car.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/orange"
  6. android:orientation="vertical">
  7. <include layout="@layout/title_shopping" />
  8. <ScrollView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content">
  11. <RelativeLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content">
  14. <LinearLayout
  15. android:id="@+id/ll_content"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:orientation="vertical"
  19. android:visibility="visible">
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:orientation="horizontal">
  24. <TextView
  25. android:layout_width="85dp"
  26. android:layout_height="wrap_content"
  27. android:gravity="center"
  28. android:text="图片"
  29. android:textColor="@color/black"
  30. android:textSize="15sp" />
  31. <TextView
  32. android:layout_width="0dp"
  33. android:layout_height="wrap_content"
  34. android:layout_weight="3"
  35. android:gravity="center"
  36. android:text="名称"
  37. android:textColor="@color/black"
  38. android:textSize="15sp" />
  39. <TextView
  40. android:layout_width="0dp"
  41. android:layout_height="wrap_content"
  42. android:layout_weight="1"
  43. android:gravity="center"
  44. android:text="数量"
  45. android:textColor="@color/black"
  46. android:textSize="15sp" />
  47. <TextView
  48. android:layout_width="0dp"
  49. android:layout_height="wrap_content"
  50. android:layout_weight="1"
  51. android:gravity="center"
  52. android:text="单价"
  53. android:textColor="@color/black"
  54. android:textSize="15sp" />
  55. <TextView
  56. android:layout_width="0dp"
  57. android:layout_height="wrap_content"
  58. android:layout_weight="1"
  59. android:gravity="center"
  60. android:text="总价"
  61. android:textColor="@color/black"
  62. android:textSize="15sp" />
  63. </LinearLayout>
  64. <LinearLayout
  65. android:id="@+id/ll_cart"
  66. android:layout_width="match_parent"
  67. android:layout_height="wrap_content"
  68. android:orientation="vertical" />
  69. <LinearLayout
  70. android:layout_width="match_parent"
  71. android:layout_height="wrap_content"
  72. android:orientation="horizontal"
  73. android:padding="0dp">
  74. <Button
  75. android:id="@+id/btn_clear"
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:gravity="center"
  79. android:text="清空"
  80. android:textColor="@color/black"
  81. android:textSize="17sp" />
  82. <TextView
  83. android:layout_width="0dp"
  84. android:layout_height="wrap_content"
  85. android:layout_weight="1"
  86. android:gravity="center|right"
  87. android:text="总金额:"
  88. android:textColor="@color/black"
  89. android:textSize="17sp" />
  90. <TextView
  91. android:id="@+id/tv_total_price"
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:layout_marginRight="10dp"
  95. android:gravity="center|left"
  96. android:textColor="@color/red"
  97. android:textSize="25sp" />
  98. <Button
  99. android:id="@+id/btn_settle"
  100. android:layout_width="wrap_content"
  101. android:layout_height="wrap_content"
  102. android:gravity="center"
  103. android:text="结算"
  104. android:textColor="@color/black"
  105. android:textSize="17sp" />
  106. </LinearLayout>
  107. </LinearLayout>
  108. <LinearLayout
  109. android:id="@+id/ll_empty"
  110. android:layout_width="match_parent"
  111. android:layout_height="wrap_content"
  112. android:orientation="vertical"
  113. android:visibility="gone"
  114. tools:visibility="visible">
  115. <TextView
  116. android:layout_width="match_parent"
  117. android:layout_height="wrap_content"
  118. android:layout_marginTop="100dp"
  119. android:layout_marginBottom="100dp"
  120. android:gravity="center"
  121. android:text="哎呀,购物车空空如也,快去选购商品吧"
  122. android:textColor="@color/black"
  123. android:textSize="17sp" />
  124. <Button
  125. android:id="@+id/btn_shopping_channel"
  126. android:layout_width="match_parent"
  127. android:layout_height="wrap_content"
  128. android:gravity="center"
  129. android:text="逛逛手机商场"
  130. android:textColor="@color/black"
  131. android:textSize="17sp" />
  132. </LinearLayout>
  133. </RelativeLayout>
  134. </ScrollView>
  135. </LinearLayout>

购物车列表清单

item_cart.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:background="@color/white"
  6. android:orientation="horizontal">
  7. <ImageView
  8. android:id="@+id/iv_thumb"
  9. android:layout_width="85dp"
  10. android:layout_height="85dp"
  11. android:scaleType="fitCenter"
  12. tools:src="@drawable/xiaomi"/>
  13. <LinearLayout
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent"
  16. android:layout_weight="3"
  17. android:orientation="vertical">
  18. <TextView
  19. android:id="@+id/tv_name"
  20. android:layout_width="match_parent"
  21. android:layout_height="0dp"
  22. android:layout_weight="2"
  23. android:gravity="left|center"
  24. android:textColor="@color/black"
  25. android:textSize="17sp"
  26. tools:text="小米手机"/>
  27. <TextView
  28. android:id="@+id/tv_desc"
  29. android:layout_width="match_parent"
  30. android:layout_height="0dp"
  31. android:layout_weight="3"
  32. android:gravity="left|center"
  33. android:textColor="@color/black"
  34. android:textSize="12sp"
  35. tools:text="小米 MI10 8GB+128GB 钛银黑 5G手机 游戏拍照手机"/>
  36. </LinearLayout>
  37. <TextView
  38. android:id="@+id/tv_count"
  39. android:layout_width="0dp"
  40. android:layout_height="match_parent"
  41. android:layout_weight="1"
  42. android:gravity="center"
  43. android:textColor="@color/black"
  44. android:textSize="17sp"
  45. tools:text="2"/>
  46. <TextView
  47. android:id="@+id/tv_price"
  48. android:layout_width="0dp"
  49. android:layout_height="match_parent"
  50. android:layout_weight="1"
  51. android:gravity="right|center"
  52. android:textColor="@color/black"
  53. android:textSize="15sp"
  54. tools:text="1000"/>
  55. <TextView
  56. android:id="@+id/tv_sum"
  57. android:layout_width="0dp"
  58. android:layout_height="match_parent"
  59. android:layout_weight="1.2"
  60. android:gravity="right|center"
  61. android:textColor="@color/red"
  62. android:textSize="17sp"
  63. tools:text="2000"/>
  64. </LinearLayout>

货物清单数据布局文件

item_goods.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/ll_item"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_gravity="center"
  7. android:background="@color/white"
  8. android:gravity="center"
  9. android:orientation="vertical">
  10. <TextView
  11. android:id="@+id/tv_name"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:gravity="center"
  15. android:textColor="@color/black"
  16. android:textSize="17sp"
  17. tools:text="小米手机" />
  18. <ImageView
  19. android:id="@+id/iv_thumb"
  20. android:layout_width="180dp"
  21. android:layout_height="150dp"
  22. android:scaleType="fitCenter"
  23. tools:src="@drawable/xiaomi" />
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="45dp"
  27. android:orientation="horizontal">
  28. <TextView
  29. android:id="@+id/tv_price"
  30. android:layout_width="0dp"
  31. android:layout_height="match_parent"
  32. android:layout_weight="2"
  33. android:gravity="center"
  34. android:textColor="@color/red"
  35. android:textSize="15sp"
  36. tools:text="20" />
  37. <Button
  38. android:id="@+id/btn_add"
  39. android:layout_width="0dp"
  40. android:layout_height="match_parent"
  41. android:layout_weight="3"
  42. android:gravity="center"
  43. android:text="加入购物车"
  44. android:textColor="@color/black"
  45. android:textSize="15sp" />
  46. </LinearLayout>
  47. </LinearLayout>

顶部布局文件

title_shopping.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="50dp"
  4. android:background="#aaaaff" >
  5. <ImageView
  6. android:id="@+id/iv_back"
  7. android:layout_width="50dp"
  8. android:layout_height="match_parent"
  9. android:layout_alignParentLeft="true"
  10. android:padding="10dp"
  11. android:scaleType="fitCenter"
  12. android:src="@drawable/ic_back" />
  13. <TextView
  14. android:id="@+id/tv_title"
  15. android:layout_width="wrap_content"
  16. android:layout_height="match_parent"
  17. android:layout_centerInParent="true"
  18. android:gravity="center"
  19. android:textColor="@color/black"
  20. android:textSize="20sp" />
  21. <ImageView
  22. android:id="@+id/iv_cart"
  23. android:layout_width="50dp"
  24. android:layout_height="match_parent"
  25. android:layout_alignParentRight="true"
  26. android:scaleType="fitCenter"
  27. android:src="@drawable/cart" />
  28. <TextView
  29. android:id="@+id/tv_count"
  30. android:layout_width="20dp"
  31. android:layout_height="20dp"
  32. android:layout_alignParentTop="true"
  33. android:layout_toRightOf="@+id/iv_cart"
  34. android:layout_marginLeft="-20dp"
  35. android:gravity="center"
  36. android:background="@drawable/shape_oval_red"
  37. android:text="0"
  38. android:textColor="@color/white"
  39. android:textSize="15sp" />
  40. </RelativeLayout>

资源文件

shape_oval_red.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="oval">
  4. <solid android:color="#ff6666" />
  5. </shape>

colors.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="purple_200">#FFBB86FC</color>
  4. <color name="purple_500">#FF6200EE</color>
  5. <color name="purple_700">#FF3700B3</color>
  6. <color name="teal_200">#FF03DAC5</color>
  7. <color name="teal_700">#FF018786</color>
  8. <color name="black">#FF000000</color>
  9. <color name="white">#FFFFFFFF</color>
  10. <color name="grey">#cccccc</color>
  11. <color name="orange">#ffffdd</color>
  12. <color name="red">#ff0000</color>
  13. </resources>

AndroidManifest.xml

把自己写的MyApplication配置上 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.kcs.shoppingcart">
  4. <application
  5. android:name=".MyApplication"
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/Theme.ShoppingCart">
  12. <activity
  13. android:name=".ShoppingCartActivity"
  14. android:exported="true" />
  15. <activity
  16. android:name=".ShoppingDetailActivity"
  17. android:exported="true" />
  18. <activity
  19. android:name=".ShoppingChannelActivity"
  20. android:exported="true">
  21. <intent-filter>
  22. <action android:name="android.intent.action.MAIN" />
  23. <category android:name="android.intent.category.LAUNCHER" />
  24. </intent-filter>
  25. </activity>
  26. </application>
  27. </manifest>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/523328
推荐阅读
相关标签
  

闽ICP备14008679号