购物车是最近电商项目开发的重要模块之一.
例子:
import java.text.DecimalFormat;
//购物车的主页面,也是view层,它实现的mvp中的view接口来获取数据
public class MainActivity extends AppCompatActivity implements IView<ShopCarBean> {
@BindView(R.id.tv_bianji)
TextView tvBianji;
@BindView(R.id.exlist)
ExpandableListView exlist;
@BindView(R.id.check_all)
public CheckBox checkAll;
@BindView(R.id.tv_zprce)
TextView price;
@BindView(R.id.tv_count)
TextView counts;
@BindView(R.id.btn_js)
Button btnJs;
//初始化一下购物车一级列表集合
ArrayList<ShopGroupBean> groupBeen = new ArrayList<>();
//初始化一下购物车一级列表集合
ArrayList<ArrayList<ShopChildBean>> goods = new ArrayList<>();
private MyShopAdapter adapter;
private boolean flagedit = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ShopCarPresenter presenter = new ShopCarPresenter(this);
//传入uid,这里是死数据
presenter.getShopData("2606");
adapter = new MyShopAdapter(this, groupBeen, goods, this);
exlist.setAdapter(adapter);
for (int i = 0; i < adapter.getGroupCount(); i++) {
exlist.expandGroup(i);
}
adapter.notifyDataSetChanged();
}
@OnClick({R.id.tv_bianji, R.id.check_all, R.id.btn_js})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.tv_bianji:
String trim = tvBianji.getText().toString().trim();
if (trim.equals("编辑")) {
tvBianji.setText("完成");
} else {
tvBianji.setText("编辑");
}
for (List<ShopChildBean> i1 : goods) {
for (int r = 0; r < i1.size(); r++) {
i1.get(r).setBtn(flagedit);
}
}
flagedit = !flagedit;
adapter.notifyDataSetChanged();
break;
case R.id.check_all:
boolean checked = checkAll.isChecked();
for (int i = 0; i < groupBeen.size(); i++) {
groupBeen.get(i).setGroupcheck(checked);
}
for (int q = 0; q < goods.size(); q++) {
ArrayList<ShopChildBean> goodsBeen = goods.get(q);
for (int j = 0; j < goodsBeen.size(); j++) {
goodsBeen.get(j).setGoodscheck(checked);
}
}
changesum(goods);
adapter.notifyDataSetChanged();
break;
case R.id.btn_js:
int index = 0;
for (int q = 0; q < goods.size(); q++) {
ArrayList<ShopChildBean> goodsBeen = goods.get(q);
for (int j = 0; j < goodsBeen.size(); j++) {
boolean goodscheck = goodsBeen.get(j).isGoodscheck();
if (goodscheck) {
index++;
}
}
}
if (index == 0) {
Toast.makeText(this, "请选择商品,谢谢", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "钱就是另一回事了", Toast.LENGTH_SHORT).show();
}
break;
}
}
DecimalFormat df = new DecimalFormat("######0.00");
@Override
public void onSuccess(ShopCarBean shopCarBean) {
if (shopCarBean != null) {
List<ShopCarBean.DataBean> data = shopCarBean.getData();
for (int i = 0; i < data.size(); i++) {
groupBeen.add(new ShopGroupBean(false, data.get(i).getSellerName(), data.get(i).getSellerid()));
List<ShopCarBean.DataBean.ListBean> list = data.get(i).getList();
ArrayList<ShopChildBean> goodsBeen = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
goodsBeen.add(new ShopChildBean(false, list.get(j).getBargainPrice(), list.get(j).getImages(), list.get(j).getTitle(), list.get(j).getSubhead(), list.get(j).getNum(), list.get(j).getPid()));
}
goods.add(goodsBeen);
}
for (int i = 0; i < adapter.getGroupCount(); i++) {
exlist.expandGroup(i);
}
adapter.notifyDataSetChanged();
}
}
@Override
public void onFiled(Throwable throwable) {
Toast.makeText(this, "shibai", Toast.LENGTH_SHORT).show();
}
public void changesum(ArrayList<ArrayList<ShopChildBean>> childBeen) {
int count = 0;
double sum = 0;
for (List<ShopChildBean> i1 : childBeen) {
for (int r = 0; r < i1.size(); r++) {
boolean childCb1 = i1.get(r).isGoodscheck();
if (childCb1) {
double price = i1.get(r).getBargainPrice();
int num = i1.get(r).getNum();
sum += price * num;
count++;
}
}
}
price.setText("¥" + df.format(sum));
counts.setText(count + "");
}
public void deleteShop(int pid) {
Toast.makeText(this, "aadas", Toast.LENGTH_SHORT).show();
}
}
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="购物车" />
<TextView
android:id="@+id/tv_bianji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="编辑" />
</RelativeLayout>
<ExpandableListView
android:id="@+id/exlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/check_all"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="全选"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="总价:"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_zprce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="0"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="数量:"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="0"
android:textSize="15dp" />
<Button
android:id="@+id/btn_js"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#fc0109"
android:text="结算"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>