电商项目中常常有购物车这个功能,做个很多项目了,都有不同的界面,选了一个来讲一下。

RecyclerView 模仿淘宝购物车功能(删除选择商品,商品计算,选择, 全选反选,商品数量加减等)

看看效果图:

Activity代码:

/*****
* RecyclerView 模仿淘宝购物车功能
*
* 删除选择商品,商品计算,选择,全选反选,商品数量加减等
*
*/
public class MainActivity extends AppCompatActivity { private RecyclerView rvNestDemo;
private CartAdapter cartAdapter;
CartInfo cartInfo;
double price;
int num; TextView cartNum;
TextView cartMoney;
Button cartShoppMoular;
CheckBox checkBox; private TextView btnDelete; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvNestDemo = (RecyclerView) findViewById(R.id.rv_nest_demo);
cartNum = findViewById(R.id.cart_num);
cartMoney = findViewById(R.id.cart_money);
cartShoppMoular = findViewById(R.id.cart_shopp_moular);
cartShoppMoular.setOnClickListener(new OnClickListener());
checkBox = findViewById(R.id.cbx_quanx_check);
checkBox.setOnClickListener(new OnClickListener());
btnDelete = (TextView) findViewById(R.id.btn_delete);
initView();
} private void initView() {
btnDelete.setOnClickListener(new OnClickListener());
showData();
rvNestDemo.setLayoutManager(new LinearLayoutManager(this));
DividerItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
itemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.line_divider_inset));
rvNestDemo.addItemDecoration(itemDecoration);
cartAdapter = new CartAdapter(this, cartInfo.getData());
rvNestDemo.setAdapter(cartAdapter);
showExpandData(); } private void showExpandData() {
/**
* 全选
*/
cartAdapter.setOnItemClickListener(new OnViewItemClickListener() {
@Override
public void onItemClick(boolean isFlang, View view, int position) {
cartInfo.getData().get(position).setIscheck(isFlang);
int length = cartInfo.getData().get(position).getItems().size();
for (int i = 0; i < length; i++) {
cartInfo.getData().get(position).getItems().get(i).setIscheck(isFlang);
}
cartAdapter.notifyDataSetChanged();
showCommodityCalculation();
}
}); /**
* 计算价钱
*/
cartAdapter.setOnItemMoneyClickListener(new OnItemMoneyClickListener() {
@Override
public void onItemClick(View view, int position) {
showCommodityCalculation();
} });
} /***
* 计算商品的数量和价格
*/
private void showCommodityCalculation() {
price = 0;
num = 0;
for (int i = 0; i < cartInfo.getData().size(); i++) {
for (int j = 0; j < cartInfo.getData().get(i).getItems().size(); j++) {
if (cartInfo.getData().get(i).getItems().get(j).ischeck()) {
price += Double.valueOf((cartInfo.getData().get(i).getItems().get(j).getNum() * Double.valueOf(cartInfo.getData().get(i).getItems().get(j).getPrice())));
num++;
} else {
checkBox.setChecked(false);
}
}
}
if (price == 0.0) {
cartNum.setText("共0件商品");
cartMoney.setText("¥ 0.0");
return;
}
try {
String money = String.valueOf(price);
cartNum.setText("共" + num + "件商品");
if (money.substring(money.indexOf("."), money.length()).length() > 2) {
cartMoney.setText("¥ " + money.substring(0, (money.indexOf(".") + 3)));
return;
}
cartMoney.setText("¥ " + money.substring(0, (money.indexOf(".") + 2)));
} catch (Exception e) {
e.printStackTrace();
}
} private void showData() {
cartInfo = JSON.parseObject(JSONDATA(), CartInfo.class);
} private class OnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) { //全选和不全选
case R.id.cbx_quanx_check:
if (checkBox.isChecked()) {
int length = cartInfo.getData().size();
for (int i = 0; i < length; i++) {
cartInfo.getData().get(i).setIscheck(true);
int lengthn = cartInfo.getData().get(i).getItems().size();
for (int j = 0; j < lengthn; j++) {
cartInfo.getData().get(i).getItems().get(j).setIscheck(true);
}
} } else {
int length = cartInfo.getData().size();
for (int i = 0; i < length; i++) {
cartInfo.getData().get(i).setIscheck(false);
int lengthn = cartInfo.getData().get(i).getItems().size();
for (int j = 0; j < lengthn; j++) {
cartInfo.getData().get(i).getItems().get(j).setIscheck(false);
}
}
}
cartAdapter.notifyDataSetChanged();
showCommodityCalculation();
break;
case R.id.btn_delete:
//删除选中商品
cartAdapter.removeChecked();
showCommodityCalculation();
break;
case R.id.cart_shopp_moular:
Toast.makeText(MainActivity.this,"提交订单: "+cartMoney.getText().toString()+"元",Toast.LENGTH_LONG).show();
break;
}
}
}

这是我写的第二个购物车,基本的功能都有了的。

有需要的小伙们,可以参考一下。

代码下载:https://github.com/DickyQie/android-shoppingcart/tree/tb-cart/

最新文章

  1. OA工作流规格--转
  2. DNS Prefetch
  3. 面试整理之DOM事件阶段
  4. 使用CSS3实现超炫的Loading(加载)动画效果
  5. Oracle删除所有表
  6. [解决]UserLibrary中的jar包不会自动发布Tomcat的lib目录下(基于MyEclipse2014)
  7. Yii自定义错误提示消息
  8. 【HDU 4612 Warm up】BCC 树的直径
  9. 【IOS学习基础】weak和strong、懒加载、循环引用
  10. Java注解(1)-注解基础
  11. Dynamics CRM2016 关于修改部署管理员账号权限引发的问题
  12. qss qt按钮自定义
  13. Pycharm 中You are using pip version 10.0.1, however version 18.1 is available. You should consider upgrading via the &#39;python -m pip install --upgrade pip&#39; command.
  14. spring拦截器中使用spring的自动注入
  15. 程序员的快速开发框架:Github上 10 大优秀的开源后台控制面板
  16. python小练习:读入一个考试得分,判断这个分数是哪个等级,并输出,考虑异常场景
  17. 使用python处理百万条数据分享(适用于java新手)
  18. Unicode Character Set and UTF-8, UTF-16, UTF-32 Encoding
  19. Tarjan 算法求强联通分量
  20. 分页导航jsp

热门文章

  1. 关于springMVC的日志管理
  2. 解决IE浏览器兼容问题的一行代码
  3. python 控制语句基础----&gt;代码块:以为冒号作为开始,用缩进来划分作用域,代表一个整体,是一个代码块,一个文件(模块)也称为一个代码块 | 作用域:作用的范围
  4. Android签名文件转化为pk8和pem来对apk重签名
  5. Maven 编译跳过检查
  6. Software Testing, Lab 1
  7. HTML5 元素属性介绍
  8. caffe-ssd的GPU安装时make test 报错:.build_release/test/test_all.testbin:
  9. Archlinux软件包管理pacman基本使用说明
  10. Python 函数中,参数是传值,还是传引用?