购物车布局xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.activity.ShopCartActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="购物车"/>
<ExpandableListView
android:id="@+id/elv"
android:groupIndicator="@null"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></ExpandableListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<CheckBox
android:id="@+id/cbAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="全选"/>
<TextView
android:id="@+id/tvMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/cbAll"
android:text="合计:"/>
<TextView
android:id="@+id/tvTotal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:textColor="#ffffff"
android:background="#ff0000"
android:gravity="center"
android:layout_marginLeft="10dp"
android:text="去结算:"/>
</RelativeLayout>
</LinearLayout>
购物车
public class ShopCartActivity extends BaseActivity<ShopcartPresenter> implements ShopcartContract.View {
private ExpandableListView mElv;
/**
* 全选
*/
private CheckBox mCbAll;
/**
* 合计:
*/
private TextView mTvMoney;
/**
* 去结算:
*/
private TextView mTvTotal;
private ProgressDialog progressDialog;
private ElvShopcartAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
//初始化dialog
progressDialog = DialogUtil.getProgressDialog(this);
String token = (String) SharedPreferencesUtils.getParam(ShopCartActivity.this, "token", "");
String uid = (String) SharedPreferencesUtils.getParam(ShopCartActivity.this, "uid", "");
mPresenter.getCarts(uid,token);
}
@Override
public int getContentLayout() {
return R.layout.activity_shop_cart;
}
@Override
public void inject() {
DaggerHttpComponent.builder()
.build()
.inject(this);
}
private void initView() {
mElv = (ExpandableListView) findViewById(R.id.elv);
mCbAll = (CheckBox) findViewById(R.id.cbAll);
mTvMoney = (TextView) findViewById(R.id.tvMoney);
mTvTotal = (TextView) findViewById(R.id.tvTotal);
mCbAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter != null) {
progressDialog.show();
adapter.changeAllState(mCbAll.isChecked());
}
}
});
mTvTotal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ShopCartActivity.this, MakeSureOrderActivity.class);
startActivity(intent);
//把用户选中的商品传过去
List<SellerBean> gList = adapter.getGroupList();
List<List<GetCartsBean.DataBean.ListBean>> cList = adapter.getchildList();
MessageEvent messageEvent = new MessageEvent();
messageEvent.setcList(cList);
messageEvent.setgList(gList);
EventBus.getDefault().postSticky(messageEvent);
}
});
}
@Override
public void showCartList(List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>> childList) {
//判断所有商家是否全部选中
mCbAll.setChecked(isSellerAddSelected(groupList));
//创建适配器
adapter = new ElvShopcartAdapter(this, groupList, childList, mPresenter,
progressDialog);
mElv.setAdapter(adapter);
//获取数量和总价
String[] strings = adapter.computeMoneyAndNum();
mTvMoney.setText("总计:" + strings[0] + "元");
mTvTotal.setText("去结算("+strings[1]+"个)");
// //默认展开列表
for (int i = 0; i < groupList.size(); i++) {
mElv.expandGroup(i);
}
//关闭进度条
progressDialog.dismiss();
}
@Override
public void updateCartsSuccess(String msg) {
if (adapter!=null){
adapter.updataSuccess();
}
}
@Override
public void deleteCartSuccess(String msg) {
//调用适配器里的delSuccess()方法
if (adapter!=null){
adapter.delSuccess();
}
}
/**
* 判断所有商家是否全部选中
*
* @param groupList
* @return
*/
private boolean isSellerAddSelected(List<SellerBean> groupList) {
for (int i = 0; i < groupList.size(); i++) {
SellerBean sellerBean = groupList.get(i);
if (!sellerBean.isSelected()) {
return false;
}
}
return true;
}
}